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"
48 {lto-2.c} {} "libdummy.a"}
50 "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" ""
51 {dummy.c} {} "lto-2.exe"}
52 {"Build libdummy.a 3a"
54 {lto-3a.c} {} "libdummy.a"}
55 {"Build libdummy.a 3c"
57 {lto-3c.c} {} "libdummy.a"}
59 "" "-flto -ffat-lto-objects"
60 {lto-3b.c} {} "liblto-3.a"}
61 {"Build libdummy.a 5a"
63 {lto-5a.c} {} "libdummy.a"}
64 {"Build libdummy.a 5b"
66 {lto-5b.c} {} "libdummy.a"}
68 "-O2 -flto -fuse-linker-plugin" ""
69 {lto-6.c} {} "lto-6.exe" "c"}
71 "" "-O2 -finline -flto"
72 {lto-9.cc} {} "libdummy.a"}
73 {"Build libdummy.a 11a"
75 {lto-11a.c} {} "libdummy.a"}
76 {"Build libdummy.a 11b"
78 {lto-11b.c} {} "libdummy.a"}
79 {"Build libdummy.a 11c"
81 {lto-11c.c} {} "libdummy.a"}
84 {lto-12c.c} {} "liblto-12.a"}
85 {"Build libdummy.a 12"
87 {lto-12a.c lto-12b.c} {} "libdummy.a"}
88 {"Build libdummy.a 13"
90 {lto-13a.c lto-13b.c} {} "libdummy.a"}
93 {lto-13c.c} {} "liblto-13.a"}
94 {"Build libdummy.a 14a"
96 {lto-14a.c lto-14b.c} {} "libdummy.a"}
99 {lto-14c.c} {} "liblto-14.a"}
100 {"Build libdummy.a 15a"
102 {lto-15a.c} {} "libdummy.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 {"Build libdummy.a PR ld/12758"
111 {pr12758a.s} {} "libdummy.a"}
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 {"Build libdummy.a PR ld/12760"
120 {pr12760a.c} {} "libdummy.a"}
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" "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"
132 {pr13183b.c} {} "libdummy.a"}
133 {"Build libdummy.a PR ld/13201"
135 {pr13201.c} {} "libdummy.a"}
137 "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto"
138 {pr13287.cc} {} "pr13287.exe" "c++"}
141 {pr15323a.c} {} "libdummy.a" "c"}
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)"
148 {pr12942a.cc pr12942c.cc} {} "libdummy.a" "c++"}
149 {"Build libdummy.a PR ld/12942 (2)"
151 {pr12942b.cc} {} "libdummy.a" "c++"}
155 # Generate input files for complex LTO tests for ELF.
156 set lto_link_elf_tests {
157 {"Build libdummy.a 7"
159 {lto-7a.c lto-7b.c lto-7c.c} {} "libdummy.a"}
161 "-shared" "-O2 -fpic"
162 {lto-7d.c} {} "liblto-7.so" "c"}
163 {"Build libdummy.a 8a"
165 {lto-8a.c} {} "libdummy.a"}
166 {"Build libdummy.a 8b"
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"}
179 "-O2 -flto -fuse-linker-plugin" "-O2 -flto"
180 {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"}
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"}
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++"}
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"
192 {pr15146a.c} {} "lib15146a.a"}
194 "-shared" "-O2 -fpic"
195 {pr15146b.c} {} "pr15146b.so" "c"}
197 "-shared -Wl,--no-as-needed tmpdir/pr15146b.so" "-O2 -fpic"
198 {pr15146c.c} {} "pr15146c.so" "c"}
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"
204 {pr15146d.c} {} "lib15146d.a"}
205 {"Build libpr15146d.a"
207 {pr15146d.c} {} "lib15146d.a"}
210 # Check final symbols in executables.
211 set lto_link_symbol_tests {
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"}
216 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
217 {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"}
219 "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" ""
220 {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"}
222 "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto"
223 {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"}
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"}
228 "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" ""
229 {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"}
232 # LTO run-time tests.
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"}
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"}
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"}
244 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
245 {dummy.c} "lto-5.exe" "lto-5.out" "" "c"}
247 "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" ""
248 {dummy.c} "lto-11.exe" "lto-11.out" "" "c"}
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"}
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"}
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"}
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"}
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"}
265 "-O2 -flto -fuse-linker-plugin" ""
266 {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"}
268 "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" ""
269 {dummy.c} "pr13201.exe" "pr13201.out" "" "c"}
271 "-O2 -flto -fuse-linker-plugin tmpdir/pr15323a.o" ""
272 {pr15323b.c} "pr15323.exe" "pr15323.out" "-flto -O2" "c"}
275 if { [at_least_gcc_version 4 7] } {
276 set lto_run_tests [concat $lto_run_tests {
278 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" ""
279 {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"}
281 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" ""
282 {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"}
286 # LTO run-time tests for ELF
287 set lto_run_elf_tests {
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"}
292 "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" ""
293 {dummy.c} "lto-8.exe" "lto-8.out" "" "c"}
295 "-O2 -flto -fuse-linker-plugin" ""
296 {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"}
299 run_cc_link_tests $lto_link_tests
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 ] } {
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]} {
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 ] } {
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"
339 run_cc_link_tests $lto_link_symbol_tests
341 # The following tests require running the executable generated by ld.
346 run_ld_link_exec_tests [] $lto_run_tests
348 if { [is_elf_format] } {
349 run_ld_link_exec_tests [] $lto_run_elf_tests