[ld/testsuite/]
[external/binutils.git] / ld / testsuite / ld-plugin / lto.exp
1 # Expect script for ld-plugin LTO tests
2 #   Copyright 2011, 2012, 2013
3 #   Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Binutils.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21
22 # These tests require plugin and LTO.
23 if { ![check_plugin_api_available]
24      || ![check_lto_available] } {
25     return
26 }
27
28 global CFLAGS
29 global CXXFLAGS
30 set saved_CFLAGS "$CFLAGS"
31 set saved_CXXFLAGS "$CXXFLAGS"
32 regsub -all "\\-Wp,-D_FORTIFY_SOURCE=\[0-9\]+" $CFLAGS "" CFLAGS
33 regsub -all "\\-Wp,-D_FORTIFY_SOURCE=\[0-9\]+" $CXXFLAGS "" CXXFLAGS
34
35 proc restore_notify { } {
36   global saved_CFLAGS
37   global saved_CXXFLAGS
38   set CFLAGS "$saved_CFLAGS"
39   set CXXFLAGS "$saved_CXXFLAGS"
40 }
41
42 # Simple LTO tests and generate input files for complex LTO tests.
43 set lto_link_tests {
44   {"LTO 1"
45    "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin"
46    {lto-1a.c lto-1b.c} {} "lto-1.exe"}
47   {"Build libdummy.a 2"
48    "" "-O2 -flto -fuse-linker-plugin"
49    {lto-2.c} {} "libdummy.a"}
50   {"LTO 2"
51    "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" ""
52    {dummy.c} {} "lto-2.exe"}
53   {"Build libdummy.a 3a"
54    "" "-flto"
55    {lto-3a.c} {} "libdummy.a"}
56   {"Build libdummy.a 3c"
57    "" "-O2"
58    {lto-3c.c} {} "libdummy.a"}
59   {"Build liblto-3.a"
60    "" "-flto -ffat-lto-objects"
61    {lto-3b.c} {} "liblto-3.a"}
62   {"Build libdummy.a 5a"
63    "" "-flto"
64    {lto-5a.c} {} "libdummy.a"}
65   {"Build libdummy.a 5b"
66    "" "-flto"
67    {lto-5b.c} {} "libdummy.a"}
68   {"LTO 6"
69    "-O2 -flto -fuse-linker-plugin" ""
70    {lto-6.c} {} "lto-6.exe" "c"}
71   {"Build libdummy.a 9"
72    "" "-O2 -finline -flto"
73    {lto-9.cc} {} "libdummy.a"}
74   {"Build libdummy.a 11a"
75    "" "-O -flto"
76    {lto-11a.c} {} "libdummy.a"}
77   {"Build libdummy.a 11b"
78    "" "-O -flto"
79    {lto-11b.c} {} "libdummy.a"}
80   {"Build libdummy.a 11c"
81    "" "-O"
82    {lto-11c.c} {} "libdummy.a"}
83   {"Build liblto-12.a"
84    "" "-O2 -flto"
85    {lto-12c.c} {} "liblto-12.a"}
86   {"Build libdummy.a 12"
87    "" "-O2 -flto"
88    {lto-12a.c lto-12b.c} {} "libdummy.a"}
89   {"Build libdummy.a 13"
90    "" "-O2 -flto"
91    {lto-13a.c lto-13b.c} {} "libdummy.a"}
92   {"Build liblto-13.a"
93    "" "-O2"
94    {lto-13c.c} {} "liblto-13.a"}
95   {"Build libdummy.a 14a"
96    "" "-flto"
97    {lto-14a.c lto-14b.c} {} "libdummy.a"}
98   {"Build liblto-14.a"
99    "" "-flto"
100    {lto-14c.c} {} "liblto-14.a"}
101   {"Build libdummy.a 15a"
102    "" "-flto"
103    {lto-15a.c} {} "libdummy.a"}
104   {"Build liblto-15.a"
105    "" "-flto"
106    {lto-15b.c} {} "liblto-15.a"}
107   {"PR ld/12696"
108    "-O2 -flto -fuse-linker-plugin -r -nostdlib" "-O2 -flto"
109    {pr12696-1.cc} {} "pr12696-1r.o" "c"}
110   {"Build libdummy.a PR ld/12758"
111    "" ""
112    {pr12758a.s} {} "libdummy.a"}
113   {"Build libpr12758.a"
114    "" "-flto -O2 -ffat-lto-objects"
115    {pr12758b.c} {} "libpr12758.a"}
116   {"PR ld/12758"
117    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12758a.o -Wl,--start-group tmpdir/libpr12758.a -Wl,--end-group" ""
118    {dummy.c} {} "pr12758.exe"}
119   {"Build libdummy.a PR ld/12760"
120    "" ""
121    {pr12760a.c} {} "libdummy.a"}
122   {"Build libpr12760.a"
123    "" "-flto -O2 -ffat-lto-objects"
124    {pr12760b.c} {} "libpr12760.a"}
125   {"PR ld/12760"
126    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12760a.o -Wl,--start-group tmpdir/libpr12760.a -Wl,--end-group" ""
127    {dummy.c} {} "pr12760.exe" "c" "warning: Bad bar"}
128   {"Build libpr13183.a"
129    "-T" "-flto -O2 -ffat-lto-objects"
130    {pr13183a.c} {} "libpr13183.a"}
131   {"Build libdummy.a PR ld/13183"
132    "" "-flto -O2"
133    {pr13183b.c} {} "libdummy.a"}
134   {"Build libdummy.a PR ld/13201"
135    "" "-flto -O2"
136    {pr13201.c} {} "libdummy.a"}
137   {"PR ld/13287"
138    "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto"
139    {pr13287.cc} {} "pr13287.exe" "c++"}
140   {"PR ld/15323"
141    "" "-O2"
142    {pr15323a.c} {} "libdummy.a" "c"}
143 }
144
145 if { [at_least_gcc_version 4 7] } {
146     set lto_link_tests [concat $lto_link_tests {
147       {"Build libdummy.a PR ld/12942 (1)"
148        "" "-flto -O2"
149        {pr12942a.cc pr12942c.cc} {} "libdummy.a" "c++"}
150       {"Build libdummy.a PR ld/12942 (2)"
151        "" "-O0"
152        {pr12942b.cc} {} "libdummy.a" "c++"}
153     }]
154 }
155
156 # Generate input files for complex LTO tests for ELF.
157 set lto_link_elf_tests {
158   {"Build libdummy.a 7"
159    "" "-flto -O2"
160    {lto-7a.c lto-7b.c lto-7c.c} {} "libdummy.a"}
161   {"Build liblto-7.so"
162    "-shared" "-O2 -fpic"
163    {lto-7d.c} {} "liblto-7.so" "c"}
164   {"Build libdummy.a 8a"
165    "" "-O2"
166    {lto-8a.c} {} "libdummy.a"}
167   {"Build libdummy.a 8b"
168    "" "-flto -O2"
169    {lto-8b.c} {} "libdummy.a"}
170   {"Build liblto-17a.so"
171    "-shared -O2 -fpic -flto -fuse-linker-plugin" "-O2 -fpic -flto"
172    {lto-17a.c} {{"nm" {} "lto-17a.d"}} "liblto-17a.so" "c"}
173   {"Build liblto-17b.so 1"
174    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto"
175    {lto-17b.c} {{"nm" {} "lto-17b-1.d"}} "liblto-17b.so"}
176   {"Build liblto-17b.so 2"
177    "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto"
178    {lto-17b.c} {{"nm" {} "lto-17b-2.d"}} "liblto-17b.so"}
179   {"PR ld/12982"
180    "-O2 -flto -fuse-linker-plugin" "-O2 -flto"
181    {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"}
182   {"PR ld/12975"
183    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib -Wl,-version-script,pr12975.t" "-O2 -flto"
184    {pr12975.c} {{"readelf" {-s --wide} "pr12975.d"}} "pr12975.so" "c"}
185   {"PR ld/13229"
186    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -finline -fno-early-inlining -flto"
187    {pr13229.cc} {{"readelf" {-s --wide} "pr13229.d"}} "pr13229.so" "c++"}
188   {"PR ld/13244"
189    "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -fno-early-inlining -flto"
190    {pr13244.c} {{"readelf" {-s --wide} "pr13244.d"}} "pr13244.so" "c"}
191   {"Build libpr15146a.a"
192    "" "-flto -O2"
193    {pr15146a.c} {} "lib15146a.a"}
194   {"Build pr15146b.so"
195    "-shared" "-O2 -fpic"
196    {pr15146b.c} {} "pr15146b.so" "c"}
197   {"Build pr15146c.so"
198    "-shared -Wl,--no-as-needed tmpdir/pr15146b.so" "-O2 -fpic"
199    {pr15146c.c} {} "pr15146c.so" "c"}
200   {"PR ld/15146 (1)"
201    "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146a.o tmpdir/pr15146c.so" ""
202    {dummy.c} {{"readelf" {-d} "pr15146.d"}} "pr15146a.exe"}
203   {"Build libpr15146d.a"
204    "" "-flto -O2"
205    {pr15146d.c} {} "lib15146d.a"}
206   {"Build libpr15146d.a"
207    "" "-flto -O2"
208    {pr15146d.c} {} "lib15146d.a"}
209 }
210
211 # Check final symbols in executables.
212 set lto_link_symbol_tests {
213   {"LTO 3 symbol"
214    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" ""
215    {dummy.c} {{"nm" {} "lto-3.d"}} "lto-3.exe" "c"}
216   {"LTO 5 symbol"
217    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
218    {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"}
219   {"LTO 9 symbol"
220    "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" ""
221    {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"}
222   {"LTO 16a symbol"
223    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto"
224    {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"}
225   {"LTO 16b symbol"
226    "-O2 -Wl,-e,foo -u bar -nostdlib -flto -fuse-linker-plugin" "-flto"
227    {lto-16a.c lto-16b.c} {{"nm" {} "lto-16b.d"}} "lto-16b.exe" "c"}
228   {"PR ld/13183"
229    "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" ""
230    {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"}
231 }
232
233 # LTO run-time tests.
234 set lto_run_tests {
235   {"LTO 3a"
236    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" ""
237    {dummy.c} "lto-3b.exe" "lto-3.out" "" "c"}
238   {"LTO 3b"
239    "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/lto-3.o" ""
240    {dummy.c} "lto-3c.exe" "lto-3.out" "" "c"}
241   {"LTO 3c"
242    "-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" ""
243    {dummy.c} "lto-3d.exe" "lto-3.out" "" "c"}
244   {"LTO 5"
245    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
246    {dummy.c} "lto-5.exe" "lto-5.out" "" "c"}
247   {"LTO 11"
248    "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" ""
249    {dummy.c} "lto-11.exe" "lto-11.out" "" "c"}
250   {"LTO 12a"
251    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/liblto-12.a tmpdir/lto-12b.o" ""
252    {dummy.c} "lto-12a.exe" "lto-12.out" "" "c"}
253   {"LTO 12b"
254    "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/lto-12b.o tmpdir/liblto-12.a" ""
255    {dummy.c} "lto-12b.exe" "lto-12.out" "" "c"}
256   {"LTO 13"
257    "-O -flto -fuse-linker-plugin tmpdir/lto-13a.o tmpdir/liblto-13.a tmpdir/lto-13b.o" ""
258    {dummy.c} "lto-13.exe" "lto-13.out" "" "c"}
259   {"LTO 14"
260    "-O2 -flto -fuse-linker-plugin tmpdir/lto-14a.o -Wl,--whole-archive tmpdir/liblto-14.a -Wl,--no-whole-archive tmpdir/lto-14b.o" ""
261    {dummy.c} "lto-14.exe" "lto-14.out" "" "c"}
262   {"LTO 15"
263    "-O2 -flto -fuse-linker-plugin -Wl,--start-group tmpdir/liblto-15.a tmpdir/lto-15a.o -Wl,--end-group" ""
264    {dummy.c} "lto-15.exe" "lto-15.out" "" "c"}
265   {"PR ld/13066"
266    "-O2 -flto -fuse-linker-plugin" ""
267    {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"}
268   {"PR ld/13201"
269    "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" ""
270    {dummy.c} "pr13201.exe" "pr13201.out" "" "c"}
271   {"PR ld/15323"
272    "-O2 -flto -fuse-linker-plugin tmpdir/pr15323a.o" ""
273    {pr15323b.c} "pr15323.exe" "pr15323.out" "-flto -O2" "c"}
274 }
275
276 if { [at_least_gcc_version 4 7] } {
277     set lto_run_tests [concat $lto_run_tests {
278       {"PR ld/12942 (1)"
279        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" ""
280        {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"}
281       {"PR ld/12942 (2)"
282        "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" ""
283        {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"}
284     }]
285 }
286
287 # LTO run-time tests for ELF
288 set lto_run_elf_tests {
289   {"LTO 7"
290    "-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" ""
291    {dummy.c} "lto-7.exe" "lto-7.out" "" "c"}
292   {"LTO 8"
293    "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" ""
294    {dummy.c} "lto-8.exe" "lto-8.out" "" "c"}
295   {"LTO TLS IE"
296    "-O2 -flto -fuse-linker-plugin" ""
297    {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"}
298 }
299
300 run_cc_link_tests $lto_link_tests
301
302 # Restrict these to ELF targets that support shared libs and PIC.
303 if { [is_elf_format]
304      && [run_host_cmd_yesno $CC "-shared -fPIC $srcdir/$subdir/dummy.c -o tmpdir/t.so"] } {
305     run_cc_link_tests $lto_link_elf_tests
306     set testname "PR ld/15146 (2)"
307     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"]
308     if { [ regexp "undefined reference to symbol 'xxx'" $exec_output ] } {
309         pass $testname
310     } {
311         fail $testname
312     }
313 }
314
315 set testname "Build liblto-11.a"
316 remote_file host delete "tmpdir/liblto-11.a"
317 set catch_output [run_host_cmd "$ar" "rc tmpdir/liblto-11.a tmpdir/lto-11a.o tmpdir/lto-11b.o tmpdir/lto-11c.o"]
318 if {![string match "" $catch_output]} {
319     unresolved $testname
320     restore_notify
321     return
322 }
323
324 if { [at_least_gcc_version 4 7] } {
325     # Check expected LTO linker errors.
326     set testname "PR ld/12942 (3)"
327     set exec_output [run_host_cmd "$CXX" "$gcc_gas_flag $gcc_ld_flag -O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
328     if { [ regexp "undefined reference to `link_error\\(\\)'" $exec_output ] } {
329         pass $testname
330     } {
331         fail $testname
332     }
333 }
334
335 # Run "ld -r" to generate inputs for complex LTO tests.
336 run_dump_test "lto-3r"
337 remote_exec host "mv" "tmpdir/dump tmpdir/lto-3.o"
338 run_dump_test "lto-5r"
339 remote_exec host "mv" "tmpdir/dump tmpdir/lto-5.o"
340
341 run_cc_link_tests $lto_link_symbol_tests
342
343 # The following tests require running the executable generated by ld.
344 if ![isnative] {
345     return
346 }
347
348 run_ld_link_exec_tests [] $lto_run_tests
349
350 if { [is_elf_format] } {
351     run_ld_link_exec_tests [] $lto_run_elf_tests
352 }
353
354 restore_notify