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