Update copyright years
[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"}
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    "" ""
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" "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 }
143
144 if { [at_least_gcc_version 4 7] } {
145     set lto_link_tests [concat $lto_link_tests {
146       {"Build libdummy.a PR ld/12942 (1)"
147        "" "-flto -O2"
148        {pr12942a.cc pr12942c.cc} {} "libdummy.a" "c++"}
149       {"Build libdummy.a PR ld/12942 (2)"
150        "" "-O0"
151        {pr12942b.cc} {} "libdummy.a" "c++"}
152     }]
153 }
154
155 # Generate input files for complex LTO tests for ELF.
156 set lto_link_elf_tests {
157   {"Build libdummy.a 7"
158    "" "-flto -O2"
159    {lto-7a.c lto-7b.c lto-7c.c} {} "libdummy.a"}
160   {"Build liblto-7.so"
161    "-shared" "-O2 -fpic"
162    {lto-7d.c} {} "liblto-7.so" "c"}
163   {"Build libdummy.a 8a"
164    "" "-O2"
165    {lto-8a.c} {} "libdummy.a"}
166   {"Build libdummy.a 8b"
167    "" "-flto -O2"
168    {lto-8b.c} {} "libdummy.a"}
169   {"Build liblto-17a.so"
170    "-shared -O2 -fpic -flto -fuse-linker-plugin" "-O2 -fpic -flto"
171    {lto-17a.c} {{"nm" {} "lto-17a.d"}} "liblto-17a.so" "c"}
172   {"Build liblto-17b.so 1"
173    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto"
174    {lto-17b.c} {{"nm" {} "lto-17b-1.d"}} "liblto-17b.so"}
175   {"Build liblto-17b.so 2"
176    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto"
177    {lto-17b.c} {{"nm" {} "lto-17b-2.d"}} "liblto-17b.so"}
178   {"PR ld/12982"
179    "-O2 -flto -fuse-linker-plugin" "-O2 -flto"
180    {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"}
181   {"PR ld/12975"
182    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib -Wl,-version-script,pr12975.t" "-O2 -flto"
183    {pr12975.c} {{"readelf" {-s --wide} "pr12975.d"}} "pr12975.so" "c"}
184   {"PR ld/13229"
185    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -finline -fno-early-inlining -flto"
186    {pr13229.cc} {{"readelf" {-s --wide} "pr13229.d"}} "pr13229.so" "c++"}
187   {"PR ld/13244"
188    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -fno-early-inlining -flto"
189    {pr13244.c} {{"readelf" {-s --wide} "pr13244.d"}} "pr13244.so" "c"}
190   {"Build libpr15146a.a"
191    "" "-flto -O2"
192    {pr15146a.c} {} "lib15146a.a"}
193   {"Build pr15146b.so"
194    "-shared" "-O2 -fpic"
195    {pr15146b.c} {} "pr15146b.so" "c"}
196   {"Build pr15146c.so"
197    "-shared -Wl,--no-as-needed tmpdir/pr15146b.so" "-O2 -fpic"
198    {pr15146c.c} {} "pr15146c.so" "c"}
199   {"PR ld/15146 (1)"
200    "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146a.o tmpdir/pr15146c.so" ""
201    {dummy.c} {{"readelf" {-d} "pr15146.d"}} "pr15146a.exe"}
202   {"Build libpr15146d.a"
203    "" "-flto -O2"
204    {pr15146d.c} {} "lib15146d.a"}
205   {"Build libpr15146d.a"
206    "" "-flto -O2"
207    {pr15146d.c} {} "lib15146d.a"}
208 }
209
210 # Check final symbols in executables.
211 set lto_link_symbol_tests {
212   {"LTO 3 symbol"
213    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" ""
214    {dummy.c} {{"nm" {} "lto-3.d"}} "lto-3.exe" "c"}
215   {"LTO 5 symbol"
216    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
217    {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"}
218   {"LTO 9 symbol"
219    "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" ""
220    {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"}
221   {"LTO 16a symbol"
222    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto"
223    {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"}
224   {"LTO 16b symbol"
225    "-O2 -Wl,-e,foo -u bar -nostdlib -flto -fuse-linker-plugin" "-flto"
226    {lto-16a.c lto-16b.c} {{"nm" {} "lto-16b.d"}} "lto-16b.exe" "c"}
227   {"PR ld/13183"
228    "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" ""
229    {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"}
230 }
231
232 # LTO run-time tests.
233 set lto_run_tests {
234   {"LTO 3a"
235    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" ""
236    {dummy.c} "lto-3b.exe" "lto-3.out" "" "c"}
237   {"LTO 3b"
238    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/lto-3.o" ""
239    {dummy.c} "lto-3c.exe" "lto-3.out" "" "c"}
240   {"LTO 3c"
241    "-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" ""
242    {dummy.c} "lto-3d.exe" "lto-3.out" "" "c"}
243   {"LTO 5"
244    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
245    {dummy.c} "lto-5.exe" "lto-5.out" "" "c"}
246   {"LTO 11"
247    "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" ""
248    {dummy.c} "lto-11.exe" "lto-11.out" "" "c"}
249   {"LTO 12a"
250    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/liblto-12.a tmpdir/lto-12b.o" ""
251    {dummy.c} "lto-12a.exe" "lto-12.out" "" "c"}
252   {"LTO 12b"
253    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/lto-12b.o tmpdir/liblto-12.a" ""
254    {dummy.c} "lto-12b.exe" "lto-12.out" "" "c"}
255   {"LTO 13"
256    "-O -flto -fuse-linker-plugin tmpdir/lto-13a.o tmpdir/liblto-13.a tmpdir/lto-13b.o" ""
257    {dummy.c} "lto-13.exe" "lto-13.out" "" "c"}
258   {"LTO 14"
259    "-O2 -flto -fuse-linker-plugin tmpdir/lto-14a.o -Wl,--whole-archive tmpdir/liblto-14.a -Wl,--no-whole-archive tmpdir/lto-14b.o" ""
260    {dummy.c} "lto-14.exe" "lto-14.out" "" "c"}
261   {"LTO 15"
262    "-O2 -flto -fuse-linker-plugin -Wl,--start-group tmpdir/liblto-15.a tmpdir/lto-15a.o -Wl,--end-group" ""
263    {dummy.c} "lto-15.exe" "lto-15.out" "" "c"}
264   {"PR ld/13066"
265    "-O2 -flto -fuse-linker-plugin" ""
266    {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"}
267   {"PR ld/13201"
268    "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" ""
269    {dummy.c} "pr13201.exe" "pr13201.out" "" "c"}
270   {"PR ld/15323"
271    "-O2 -flto -fuse-linker-plugin tmpdir/pr15323a.o" ""
272    {pr15323b.c} "pr15323.exe" "pr15323.out" "-flto -O2" "c"}
273 }
274
275 if { [at_least_gcc_version 4 7] } {
276     set lto_run_tests [concat $lto_run_tests {
277       {"PR ld/12942 (1)"
278        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" ""
279        {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"}
280       {"PR ld/12942 (2)"
281        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" ""
282        {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"}
283     }]
284 }
285
286 # LTO run-time tests for ELF
287 set lto_run_elf_tests {
288   {"LTO 7"
289    "-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" ""
290    {dummy.c} "lto-7.exe" "lto-7.out" "" "c"}
291   {"LTO 8"
292    "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" ""
293    {dummy.c} "lto-8.exe" "lto-8.out" "" "c"}
294   {"LTO TLS IE"
295    "-O2 -flto -fuse-linker-plugin" ""
296    {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"}
297 }
298
299 run_cc_link_tests $lto_link_tests
300
301 # Restrict these to ELF targets that support shared libs and PIC.
302 if { [is_elf_format] && [check_lto_shared_available] } {
303     run_cc_link_tests $lto_link_elf_tests
304     set testname "PR ld/15146 (2)"
305     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"]
306     if { [ regexp "undefined reference to symbol 'xxx'" $exec_output ] } {
307         pass $testname
308     } {
309         fail $testname
310     }
311 }
312
313 set testname "Build liblto-11.a"
314 remote_file host delete "tmpdir/liblto-11.a"
315 set catch_output [run_host_cmd "$ar" "rc tmpdir/liblto-11.a tmpdir/lto-11a.o tmpdir/lto-11b.o tmpdir/lto-11c.o"]
316 if {![string match "" $catch_output]} {
317     unresolved $testname
318     restore_notify
319     return
320 }
321
322 if { [at_least_gcc_version 4 7] } {
323     # Check expected LTO linker errors.
324     set testname "PR ld/12942 (3)"
325     set exec_output [run_host_cmd "$CXX" "$gcc_gas_flag $gcc_ld_flag -O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
326     if { [ regexp "undefined reference to `link_error\\(\\)'" $exec_output ] } {
327         pass $testname
328     } {
329         fail $testname
330     }
331 }
332
333 # Run "ld -r" to generate inputs for complex LTO tests.
334 run_dump_test "lto-3r"
335 remote_exec host "mv" "tmpdir/dump tmpdir/lto-3.o"
336 run_dump_test "lto-5r"
337 remote_exec host "mv" "tmpdir/dump tmpdir/lto-5.o"
338
339 run_cc_link_tests $lto_link_symbol_tests
340
341 # The following tests require running the executable generated by ld.
342 if ![isnative] {
343     return
344 }
345
346 run_ld_link_exec_tests [] $lto_run_tests
347
348 if { [is_elf_format] } {
349     run_ld_link_exec_tests [] $lto_run_elf_tests
350 }
351
352 restore_notify