1 # Expect script for ld-plugin LTO tests
2 # Copyright (C) 2011-2014 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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.
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.
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,
21 # These tests require plugin and LTO.
22 if { ![check_plugin_api_available]
23 || ![check_lto_available] } {
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
34 proc restore_notify { } {
37 set CFLAGS "$saved_CFLAGS"
38 set CXXFLAGS "$saved_CXXFLAGS"
41 # Simple LTO tests and generate input files for complex LTO tests.
44 "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin"
45 {lto-1a.c lto-1b.c} {} "lto-1.exe"}
47 "" "-O2 -flto -fuse-linker-plugin"
50 "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" ""
51 {dummy.c} {} "lto-2.exe"}
59 "" "-flto -ffat-lto-objects"
60 {lto-3b.c} {} "liblto-3.a"}
68 "-O2 -flto -fuse-linker-plugin" ""
69 {lto-6.c} {} "lto-6.exe" "c"}
71 "" "-O2 -finline -flto"
72 {lto-9.cc} {} "" "c++"}
84 {lto-12c.c} {} "liblto-12.a"}
87 {lto-12a.c lto-12b.c} {} ""}
90 {lto-13a.c lto-13b.c} {} ""}
93 {lto-13c.c} {} "liblto-13.a"}
96 {lto-14a.c lto-14b.c} {} ""}
99 {lto-14c.c} {} "liblto-14.a"}
105 {lto-15b.c} {} "liblto-15.a"}
107 "-O2 -flto -fuse-linker-plugin -r -nostdlib" "-O2 -flto"
108 {pr12696-1.cc} {} "pr12696-1r.o" "c++"}
109 {"Compile PR ld/12758"
112 {"Build libpr12758.a"
113 "" "-flto -O2 -ffat-lto-objects"
114 {pr12758b.c} {} "libpr12758.a"}
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 {"Compile PR ld/12760"
121 {"Build libpr12760.a"
122 "" "-flto -O2 -ffat-lto-objects"
123 {pr12760b.c} {} "libpr12760.a"}
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 {"Compile PR ld/13183"
133 {"Compile PR ld/13201"
137 "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto"
138 {pr13287.cc} {} "pr13287.exe" "c++"}
141 {pr15323a.c} {} "" "c"}
142 {"Compile(1) PR ld/pr16846"
144 {pr16846a.c pr16846b.c} {} ""}
145 {"Compile(2) PR ld/pr16846"
149 "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846b.o tmpdir/pr16846c.o" ""
150 {dummy.c} {} "pr16846a.exe"}
152 "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846c.o tmpdir/pr16846b.o" ""
153 {dummy.c} {} "pr16846b.exe"}
156 if { [at_least_gcc_version 4 7] } {
157 set lto_link_tests [concat $lto_link_tests {
158 {"Compile PR ld/12942 (1)"
160 {pr12942a.cc pr12942c.cc} {} "" "c++"}
161 {"Compile PR ld/12942 (2)"
163 {pr12942b.cc} {} "" "c++"}
167 # Generate input files for complex LTO tests for ELF.
168 set lto_link_elf_tests {
171 {lto-7a.c lto-7b.c lto-7c.c} {} ""}
173 "-shared" "-O2 -fpic"
174 {lto-7d.c} {} "liblto-7.so" "c"}
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"}
191 "-O2 -flto -fuse-linker-plugin" "-O2 -flto"
192 {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"}
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"}
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++"}
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"
204 {pr15146a.c} {} "lib15146a.a"}
206 "-shared" "-O2 -fpic"
207 {pr15146b.c} {} "pr15146b.so" "c"}
209 "-shared -Wl,--no-as-needed tmpdir/pr15146b.so" "-O2 -fpic"
210 {pr15146c.c} {} "pr15146c.so" "c"}
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"
216 {pr15146d.c} {} "lib15146d.a"}
217 {"Build libpr15146d.a"
219 {pr15146d.c} {} "lib15146d.a"}
222 # Check final symbols in executables.
223 set lto_link_symbol_tests {
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"}
228 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
229 {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"}
231 "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" ""
232 {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"}
234 "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto"
235 {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"}
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"}
240 "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" ""
241 {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"}
244 # LTO run-time tests.
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"}
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"}
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"}
256 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
257 {dummy.c} "lto-5.exe" "lto-5.out" "" "c"}
259 "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" ""
260 {dummy.c} "lto-11.exe" "lto-11.out" "" "c"}
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"}
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"}
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"}
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"}
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"}
277 "-O2 -flto -fuse-linker-plugin" ""
278 {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"}
280 "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" ""
281 {dummy.c} "pr13201.exe" "pr13201.out" "" "c"}
283 "-O2 -flto -fuse-linker-plugin tmpdir/pr15323a.o" ""
284 {pr15323b.c} "pr15323.exe" "pr15323.out" "-flto -O2" "c"}
287 if { [at_least_gcc_version 4 7] } {
288 set lto_run_tests [concat $lto_run_tests {
290 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" ""
291 {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"}
293 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" ""
294 {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"}
298 # LTO run-time tests for ELF
299 set lto_run_elf_tests {
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"}
304 "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" ""
305 {dummy.c} "lto-8.exe" "lto-8.out" "" "c"}
307 "-O2 -flto -fuse-linker-plugin" ""
308 {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"}
311 run_cc_link_tests $lto_link_tests
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" "-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 ] } {
325 set testname "Build liblto-11.a"
326 remote_file host delete "tmpdir/liblto-11.a"
333 foreach plug $plugin_names {
334 set plug_so [run_host_cmd $CC "--print-prog-name $plug"]
335 if { $plug_so ne $plug } then {
336 set plug_opt "--plugin $plug_so"
340 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"]
341 if {![string match "" $catch_output]} {
347 if { [at_least_gcc_version 4 7] } {
348 # Check expected LTO linker errors.
349 set testname "PR ld/12942 (3)"
350 set exec_output [run_host_cmd "$CXX" "-O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
351 if { [ regexp "undefined reference to `link_error\\(\\)'" $exec_output ] } {
358 # Run "ld -r" to generate inputs for complex LTO tests.
359 run_dump_test "lto-3r"
360 remote_exec host "mv" "tmpdir/dump tmpdir/lto-3.o"
361 run_dump_test "lto-5r"
362 remote_exec host "mv" "tmpdir/dump tmpdir/lto-5.o"
364 run_cc_link_tests $lto_link_symbol_tests
366 # The following tests require running the executable generated by ld.
371 run_ld_link_exec_tests [] $lto_run_tests
373 if { [is_elf_format] } {
374 run_ld_link_exec_tests [] $lto_run_elf_tests