1 # Expect script for ld-plugin LTO tests
3 # Free Software Foundation, Inc.
5 # This file is part of the GNU Binutils.
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.
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.
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,
22 # These tests require plugin and LTO.
23 if { ![check_plugin_api_available]
24 || ![check_lto_available] } {
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
35 proc restore_notify { } {
38 set CFLAGS "$saved_CFLAGS"
39 set CXXFLAGS "$saved_CXXFLAGS"
42 # Simple LTO tests and generate input files for complex LTO tests.
45 "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin"
46 {lto-1a.c lto-1b.c} {} "lto-1.exe"}
48 "" "-O2 -flto -fuse-linker-plugin"
49 {lto-2.c} {} "libdummy.a"}
51 "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" ""
52 {dummy.c} {} "lto-2.exe"}
53 {"Build libdummy.a 3a"
55 {lto-3a.c} {} "libdummy.a"}
56 {"Build libdummy.a 3c"
58 {lto-3c.c} {} "libdummy.a"}
61 {lto-3b.c} {} "liblto-3.a"}
62 {"Build libdummy.a 5a"
64 {lto-5a.c} {} "libdummy.a"}
65 {"Build libdummy.a 5b"
67 {lto-5b.c} {} "libdummy.a"}
69 "-O2 -flto -fuse-linker-plugin" ""
70 {lto-6.c} {} "lto-6.exe" "c"}
72 "" "-O2 -finline -flto"
73 {lto-9.cc} {} "libdummy.a"}
74 {"Build libdummy.a 11a"
76 {lto-11a.c} {} "libdummy.a"}
77 {"Build libdummy.a 11b"
79 {lto-11b.c} {} "libdummy.a"}
80 {"Build libdummy.a 11c"
82 {lto-11c.c} {} "libdummy.a"}
85 {lto-12c.c} {} "liblto-12.a"}
86 {"Build libdummy.a 12"
88 {lto-12a.c lto-12b.c} {} "libdummy.a"}
89 {"Build libdummy.a 13"
91 {lto-13a.c lto-13b.c} {} "libdummy.a"}
94 {lto-13c.c} {} "liblto-13.a"}
95 {"Build libdummy.a 14a"
97 {lto-14a.c lto-14b.c} {} "libdummy.a"}
100 {lto-14c.c} {} "liblto-14.a"}
101 {"Build libdummy.a 15a"
103 {lto-15a.c} {} "libdummy.a"}
106 {lto-15b.c} {} "liblto-15.a"}
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"
112 {pr12758a.s} {} "libdummy.a"}
113 {"Build libpr12758.a"
115 {pr12758b.c} {} "libpr12758.a"}
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"
121 {pr12760a.c} {} "libdummy.a"}
122 {"Build libpr12760.a"
124 {pr12760b.c} {} "libpr12760.a"}
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 libdummy.a PR ld/12942 (1)"
130 {pr12942a.cc pr12942c.cc} {} "libdummy.a" "c++"}
131 {"Build libdummy.a PR ld/12942 (2)"
133 {pr12942b.cc} {} "libdummy.a" "c++"}
134 {"Build libpr13183.a"
136 {pr13183a.c} {} "libpr13183.a"}
137 {"Build libdummy.a PR ld/13183"
139 {pr13183b.c} {} "libdummy.a"}
140 {"Build libdummy.a PR ld/13201"
142 {pr13201.c} {} "libdummy.a"}
144 "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto"
145 {pr13287.cc} {} "pr13287.exe" "c++"}
148 # Generate input files for complex LTO tests for ELF.
149 set lto_link_elf_tests {
150 {"Build libdummy.a 7"
152 {lto-7a.c lto-7b.c lto-7c.c} {} "libdummy.a"}
154 "-shared" "-O2 -fpic"
155 {lto-7d.c} {} "liblto-7.so" "c"}
156 {"Build libdummy.a 8a"
158 {lto-8a.c} {} "libdummy.a"}
159 {"Build libdummy.a 8b"
161 {lto-8b.c} {} "libdummy.a"}
162 {"Build liblto-17a.so"
163 "-shared -O2 -fpic -flto -fuse-linker-plugin" "-O2 -fpic -flto"
164 {lto-17a.c} {{"nm" {} "lto-17a.d"}} "liblto-17a.so" "c"}
165 {"Build liblto-17b.so 1"
166 "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto"
167 {lto-17b.c} {{"nm" {} "lto-17b-1.d"}} "liblto-17b.so"}
168 {"Build liblto-17b.so 2"
169 "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto"
170 {lto-17b.c} {{"nm" {} "lto-17b-2.d"}} "liblto-17b.so"}
172 "-O2 -flto -fuse-linker-plugin" "-O2 -flto"
173 {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"}
175 "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib -Wl,-version-script,pr12975.t" "-O2 -flto"
176 {pr12975.c} {{"readelf" {-s --wide} "pr12975.d"}} "pr12975.so" "c"}
178 "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -finline -fno-early-inlining -flto"
179 {pr13229.cc} {{"readelf" {-s --wide} "pr13229.d"}} "pr13229.so" "c++"}
181 "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -fno-early-inlining -flto"
182 {pr13244.c} {{"readelf" {-s --wide} "pr13244.d"}} "pr13244.so" "c"}
183 {"Build libpr15146a.a"
185 {pr15146a.c} {} "lib15146a.a"}
187 "-shared" "-O2 -fpic"
188 {pr15146b.c} {} "pr15146b.so" "c"}
190 "-shared -Wl,--no-as-needed tmpdir/pr15146b.so" "-O2 -fpic"
191 {pr15146c.c} {} "pr15146c.so" "c"}
193 "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146a.o tmpdir/pr15146c.so" ""
194 {dummy.c} {{"readelf" {-d} "pr15146.d"}} "pr15146a.exe"}
195 {"Build libpr15146d.a"
197 {pr15146d.c} {} "lib15146d.a"}
198 {"Build libpr15146d.a"
200 {pr15146d.c} {} "lib15146d.a"}
203 # Check final symbols in executables.
204 set lto_link_symbol_tests {
206 "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" ""
207 {dummy.c} {{"nm" {} "lto-3.d"}} "lto-3.exe" "c"}
209 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
210 {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"}
212 "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" ""
213 {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"}
215 "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto"
216 {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"}
218 "-O2 -Wl,-e,foo -u bar -nostdlib -flto -fuse-linker-plugin" "-flto"
219 {lto-16a.c lto-16b.c} {{"nm" {} "lto-16b.d"}} "lto-16b.exe" "c"}
221 "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" ""
222 {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"}
225 # LTO run-time tests.
228 "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" ""
229 {dummy.c} "lto-3b.exe" "lto-3.out" "" "c"}
231 "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/lto-3.o" ""
232 {dummy.c} "lto-3c.exe" "lto-3.out" "" "c"}
234 "-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" ""
235 {dummy.c} "lto-3d.exe" "lto-3.out" "" "c"}
237 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
238 {dummy.c} "lto-5.exe" "lto-5.out" "" "c"}
240 "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" ""
241 {dummy.c} "lto-11.exe" "lto-11.out" "" "c"}
243 "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/liblto-12.a tmpdir/lto-12b.o" ""
244 {dummy.c} "lto-12a.exe" "lto-12.out" "" "c"}
246 "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/lto-12b.o tmpdir/liblto-12.a" ""
247 {dummy.c} "lto-12b.exe" "lto-12.out" "" "c"}
249 "-O -flto -fuse-linker-plugin tmpdir/lto-13a.o tmpdir/liblto-13.a tmpdir/lto-13b.o" ""
250 {dummy.c} "lto-13.exe" "lto-13.out" "" "c"}
252 "-O2 -flto -fuse-linker-plugin tmpdir/lto-14a.o -Wl,--whole-archive tmpdir/liblto-14.a -Wl,--no-whole-archive tmpdir/lto-14b.o" ""
253 {dummy.c} "lto-14.exe" "lto-14.out" "" "c"}
255 "-O2 -flto -fuse-linker-plugin -Wl,--start-group tmpdir/liblto-15.a tmpdir/lto-15a.o -Wl,--end-group" ""
256 {dummy.c} "lto-15.exe" "lto-15.out" "" "c"}
258 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" ""
259 {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"}
261 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" ""
262 {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"}
264 "-O2 -flto -fuse-linker-plugin" ""
265 {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"}
267 "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" ""
268 {dummy.c} "pr13201.exe" "pr13201.out" "" "c"}
271 # LTO run-time tests for ELF
272 set lto_run_elf_tests {
274 "-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" ""
275 {dummy.c} "lto-7.exe" "lto-7.out" "" "c"}
277 "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" ""
278 {dummy.c} "lto-8.exe" "lto-8.out" "" "c"}
280 "-O2 -flto -fuse-linker-plugin" ""
281 {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"}
284 run_cc_link_tests $lto_link_tests
286 # Restrict these to ELF targets that support shared libs and PIC.
288 && [run_host_cmd_yesno $CC "-shared -fPIC $srcdir/$subdir/dummy.c -o tmpdir/t.so"] } {
289 run_cc_link_tests $lto_link_elf_tests
290 set testname "PR ld/15146 (2)"
291 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"]
292 if { [ regexp "ltrans.o: undefined reference to symbol 'xxx'" $exec_output ] } {
299 set testname "Build liblto-11.a"
300 remote_file host delete "tmpdir/liblto-11.a"
301 set catch_output [run_host_cmd "$ar" "rc tmpdir/liblto-11.a tmpdir/lto-11a.o tmpdir/lto-11b.o tmpdir/lto-11c.o"]
302 if {![string match "" $catch_output]} {
308 # Check expected LTO linker errors.
309 set testname "PR ld/12942 (3)"
310 set exec_output [run_host_cmd "$CXX" "$gcc_gas_flag $gcc_ld_flag -O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
311 if { [ regexp "pr12942a.h:7: undefined reference to `link_error\\(\\)'" $exec_output ] } {
317 # Run "ld -r" to generate inputs for complex LTO tests.
318 run_dump_test "lto-3r"
319 remote_exec host "mv" "tmpdir/dump tmpdir/lto-3.o"
320 run_dump_test "lto-5r"
321 remote_exec host "mv" "tmpdir/dump tmpdir/lto-5.o"
323 run_cc_link_tests $lto_link_symbol_tests
325 # The following tests require running the executable generated by ld.
330 run_ld_link_exec_tests [] $lto_run_tests
332 if { [is_elf_format] } {
333 run_ld_link_exec_tests [] $lto_run_elf_tests