684d1db314ad8bfe8e4256e5c3cff9221a7c716c
[binutils-gdb.git] / ld / testsuite / ld-plugin / lto.exp
1 # Expect script for ld-plugin LTO tests
2 # Copyright (C) 2011-2020 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 # Check to see if the C and C++ compilers work
22 if { ![check_compiler_available] || [which $CXX] == 0 } {
23 return
24 }
25
26 # These tests require plugin and LTO.
27 if { ![check_plugin_api_available]
28 || ![check_lto_available] } {
29 return
30 }
31
32 set saved_CFLAGS "$CFLAGS"
33 set saved_CXXFLAGS "$CXXFLAGS"
34 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CFLAGS "" CFLAGS
35 regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CXXFLAGS "" CXXFLAGS
36
37 proc restore_notify { } {
38 global saved_CFLAGS
39 global saved_CXXFLAGS
40 set CFLAGS "$saved_CFLAGS"
41 set CXXFLAGS "$saved_CXXFLAGS"
42 }
43
44 set plugin_names {
45 liblto_plugin.so
46 liblto_plugin-0.dll
47 cyglto_plugin-0.dll
48 }
49 set plug_opt ""
50 foreach plug $plugin_names {
51 set plug_so [run_host_cmd $CC "--print-prog-name $plug"]
52 if { $plug_so eq $plug } then {
53 set plug_so [run_host_cmd $CC "--print-file-name $plug"]
54 }
55 if { $plug_so ne $plug } then {
56 set plug_opt "--plugin $plug_so"
57 break
58 }
59 }
60 set lto_fat ""
61 set lto_no_fat ""
62 if { [check_lto_fat_available] } {
63 set lto_fat "-ffat-lto-objects"
64 set lto_no_fat "-fno-fat-lto-objects"
65 set no_lto "-fno-lto"
66 }
67
68 # Simple LTO tests and generate input files for complex LTO tests.
69 set lto_link_tests [list \
70 [list "LTO 1" \
71 "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin" \
72 {lto-1a.c lto-1b.c} {} "lto-1.exe"] \
73 [list "Compile 2" \
74 "" "-O2 -flto -fuse-linker-plugin $NOSANITIZE_CFLAGS" \
75 {lto-2.c} {} ""] \
76 [list "LTO 2" \
77 "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" \
78 "$NOSANITIZE_CFLAGS" \
79 {dummy.c} {} "lto-2.exe"] \
80 [list "Compile 3a" \
81 "" "-flto" \
82 {lto-3a.c} {} ""] \
83 [list "Compile 3c" \
84 "" "-O2" \
85 {lto-3c.c} {} ""] \
86 [list "Build liblto-3.a" \
87 "" "-flto $lto_fat" \
88 {lto-3b.c} {} "liblto-3.a"] \
89 [list "Compile 5a" \
90 "" "-flto $lto_fat" \
91 {lto-5a.c} {} ""] \
92 [list "Compile 5b" \
93 "" "-flto $lto_fat" \
94 {lto-5b.c} {} ""] \
95 [list "Compile PR ld/12365" \
96 "" "-flto -O2 $lto_fat" \
97 {pr12365a.c pr12365b.c pr12365c.c} {} ""] \
98 [list "Compile 9" \
99 "" "-O2 -finline -flto" \
100 {lto-9.cc} {} "" "c++"] \
101 [list "Compile 11a" \
102 "" "-O -flto" \
103 {lto-11a.c} {} ""] \
104 [list "Compile 11b" \
105 "" "-O -flto" \
106 {lto-11b.c} {} ""] \
107 [list "Compile 11c" \
108 "" "-O" \
109 {lto-11c.c} {} ""] \
110 [list "Build liblto-12.a" \
111 "$plug_opt" "-O2 -flto" \
112 {lto-12c.c} {} "liblto-12.a"] \
113 [list "Compile 12" \
114 "" "-O2 -flto" \
115 {lto-12a.c lto-12b.c} {} ""] \
116 [list "Compile 13" \
117 "" "-O2 -flto" \
118 {lto-13a.c lto-13b.c} {} ""] \
119 [list "Build liblto-13.a" \
120 "" "-O2" \
121 {lto-13c.c} {} "liblto-13.a"] \
122 [list "Compile 14a" \
123 "" "-flto" \
124 {lto-14a.c lto-14b.c} {} ""] \
125 [list "Build liblto-14.a" \
126 "$plug_opt" "-flto" \
127 {lto-14c.c} {} "liblto-14.a"] \
128 [list "Compile 15a" \
129 "" "-flto" \
130 {lto-15a.c} {} ""] \
131 [list "Build liblto-15.a" \
132 "$plug_opt" "-flto" \
133 {lto-15b.c} {} "liblto-15.a"] \
134 [list "PR ld/12696" \
135 "-O2 -flto -fuse-linker-plugin -r -nostdlib" "-O2 -flto" \
136 {pr12696-1.cc} {} "pr12696-1r.o" "c++"] \
137 [list "Compile PR ld/12758" \
138 "" "" \
139 {pr12758a.s} {} ""] \
140 [list "Build libpr12758.a" \
141 "" "-flto -O2 $lto_fat $NOSANITIZE_CFLAGS" \
142 {pr12758b.c} {} "libpr12758.a"] \
143 [list "PR ld/12758" \
144 "$NOPIE_LDFLAGS $NOSANITIZE_CFLAGS -O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12758a.o -Wl,--start-group tmpdir/libpr12758.a -Wl,--end-group" \
145 "$NOSANITIZE_CFLAGS" \
146 {dummy.c} {} "pr12758.exe"] \
147 [list "Build libpr13183.a" \
148 "-T" "-flto -O2 $lto_fat" \
149 {pr13183a.c} {} "libpr13183.a"] \
150 [list "Compile PR ld/13183" \
151 "" "-flto -O2" \
152 {pr13183b.c} {} ""] \
153 [list "Compile PR ld/13201" \
154 "" "-flto -O2" \
155 {pr13201.c} {} ""] \
156 [list "PR ld/13287" \
157 "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto" \
158 {pr13287.cc} {} "pr13287.exe" "c++"] \
159 [list "PR ld/15323 (1)" \
160 "" "-O2" \
161 {pr15323a.c} {} "" "c"] \
162 [list "PR ld/15323 (2)" \
163 "-O2 -flto -r -nostdlib" "-O2 -flto" \
164 {pr15323a.c} {} "pr15323a-r.o" "c"] \
165 [list "Compile(1) PR ld/pr16846" \
166 "" "-flto" \
167 {pr16846a.c pr16846b.c} {} ""] \
168 [list "Compile(2) PR ld/pr16846" \
169 "" "" \
170 {pr16846c.c} {} ""] \
171 [list "PR ld/pr16846(1)" \
172 "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846b.o tmpdir/pr16846c.o" "" \
173 {dummy.c} {} "pr16846a.exe"] \
174 [list "PR ld/pr16846(2)" \
175 "-flto -fuse-linker-plugin tmpdir/pr16846a.o tmpdir/pr16846c.o tmpdir/pr16846b.o" "" \
176 {dummy.c} {} "pr16846b.exe"] \
177 [list "PR ld/19317 (1)" \
178 "$plug_opt" "-flto $lto_no_fat" \
179 {pr19317.c} {} "libpr19317.a"] \
180 [list "Build pr20276a.o" \
181 "" "-fno-lto -fcommon" \
182 {pr20276a.c}] \
183 [list "Build pr20276b.o" \
184 "$plug_opt" "-flto $lto_no_fat -fcommon" \
185 {pr20276b.c}] \
186 [list "Build pr20267a.o" \
187 "" "-fcommon" \
188 {pr20267a.c}] \
189 [list "Build libpr20267a.a" \
190 "$plug_opt" "-flto $lto_fat -fcommon" \
191 {pr20267b.c} {} "libpr20267a.a"] \
192 [list "Build libpr20267b.a" \
193 "$plug_opt" "-flto $lto_no_fat -fcommon" \
194 {pr20267b.c} {} "libpr20267b.a"] \
195 [list "Build pr20321" \
196 "-flto -Wl,-plugin,$plug_so" "-flto" \
197 {pr20321.c} {{warning ".*: duplicated plugin"}} \
198 "pr20321" "c"] \
199 [list "Build pr22502a.o" \
200 "" "" \
201 {pr22502a.c}] \
202 [list "Build pr22502b.o" \
203 "$plug_opt" "-flto $lto_no_fat" \
204 {pr22502b.c}] \
205 [list "Build pr22751.a" \
206 "$plug_opt" "-flto $lto_no_fat $INT128_CFLAGS" \
207 {pr22751.c} {} "pr22751.a"] \
208 [list "Build pr24406-2b.o" \
209 "" "-O2 -fno-lto" \
210 {pr24406-2b.c}] \
211 [list "Build pr26163a.o" \
212 "" "-O2 -fno-lto" \
213 {pr26163a.c}] \
214 [list "Build pr26262b.o" \
215 "" "-O2" \
216 {pr26262b.c} {} "" "c"] \
217 [list "Build pr26262c.o" \
218 "" "-O2" \
219 {pr26262c.c} {} "" "c"] \
220 [list "Build pr26267a.o" \
221 "" "-O2 -flto $lto_no_fat" \
222 {pr26267a.c} {} "" "c"] \
223 [list "Build pr26267b.o" \
224 "" "-O2" \
225 {pr26267b.c} {} "" "c"] \
226 [list "Build pr26267c.o" \
227 "" "-O2" \
228 {pr26267c.c} {} "" "c"] \
229 [list "Build pr26267a" \
230 "" "-O2" \
231 {pr26267a.c} {} "" "c"] \
232 [list "Build pr26267a" \
233 "-flto tmpdir/pr26267a.o tmpdir/pr26267b.o tmpdir/pr26267c.o" \
234 "-flto $lto_no_fat" \
235 {dummy.c} \
236 {{error_output "pr26267.err"}} \
237 "pr26267a"] \
238 [list "Build pr26267b" \
239 "-flto tmpdir/pr26267b.o tmpdir/pr26267c.o tmpdir/pr26267a.o" \
240 "-flto $lto_no_fat" \
241 {dummy.c} \
242 {{error_output "pr26267.err"}} \
243 "pr26267b"] \
244 [list "Build pr26389.o" \
245 "$plug_opt" "-flto $lto_no_fat -fcommon" \
246 {pr26389.c} \
247 [list [list "nm" "$plug_opt" "pr26389.d"]]] \
248 ]
249
250 if { [at_least_gcc_version 10 0] } {
251 set lto_link_tests [concat $lto_link_tests [list \
252 [list "pr25355.o" \
253 "" \
254 "-flto -fno-common $lto_no_fat" \
255 {pr25355.c} \
256 [list [list "nm" "$plug_opt" "pr25355.d"]]] \
257 ]]
258 }
259
260 if { [at_least_gcc_version 4 7] } {
261 set lto_link_tests [concat $lto_link_tests [list \
262 [list "Compile PR ld/12942 (1)" \
263 "" "-flto -O2" \
264 {pr12942a.cc pr12942c.cc} {} "" "c++"] \
265 [list "Compile PR ld/12942 (2)" \
266 "" "-O0" \
267 {pr12942b.cc} {} "" "c++"] \
268 ]]
269 }
270
271 set lto_compile_elf_tests [list \
272 [list "Compile 7" \
273 "" "-flto -O2" \
274 {lto-7a.c lto-7b.c lto-7c.c} {} ""] \
275 [list "Compile 8a" \
276 "" "-O2" \
277 {lto-8a.c} {} ""] \
278 [list "Compile 8b" \
279 "" "-flto -O2" \
280 {lto-8b.c} {} ""] \
281 [list "PR ld/23309" \
282 "-O2 -flto -fuse-linker-plugin -fvisibility=hidden -ffunction-sections -Wl,--gc-sections,-u,KeepMe" \
283 "-O2 -flto -fuse-linker-plugin -fvisibility=hidden -ffunction-sections" \
284 {pr23309.c} {{"nm" {} "pr23309.d"}} "pr23309.exe" "c"] \
285 ]
286
287 # Generate input files for complex LTO tests for ELF.
288 set lto_link_elf_tests [list \
289 [list "LTO 6" \
290 "-O2 -flto -fuse-linker-plugin" "" \
291 {lto-6.c} {} "lto-6.exe" "c"] \
292 [list "Build liblto-7.so" \
293 "-shared" "-O2 -fpic" \
294 {lto-7d.c} {} "liblto-7.so" "c"] \
295 [list "Build liblto-17a.so" \
296 "-shared -O2 -fpic -flto -fuse-linker-plugin" "-O2 -fpic -flto" \
297 {lto-17a.c} {{"nm" {} "lto-17a.d"}} "liblto-17a.so" "c"] \
298 [list "Build liblto-17b.so 1" \
299 "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto" \
300 {lto-17b.c} {{"nm" {} "lto-17b-1.d"}} "liblto-17b.so"] \
301 [list "Build liblto-17b.so 2" \
302 "-shared -O2 -fpic -flto -fuse-linker-plugin tmpdir/lto-17a.o" "-O2 -fpic -flto" \
303 {lto-17b.c} {{"nm" {} "lto-17b-2.d"}} "liblto-17b.so"] \
304 [list "Compile PR ld/12760" \
305 "" "-g -O0" \
306 {pr12760a.c} {} ""] \
307 [list "Build libpr12760.a" \
308 "" "-flto -O2 $lto_fat" \
309 {pr12760b.c} {} "libpr12760.a"] \
310 [list "PR ld/12760" \
311 "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin tmpdir/pr12760a.o -Wl,--start-group tmpdir/libpr12760.a -Wl,--end-group" "" \
312 {dummy.c} {{warning "pr12760a.c:6: warning: Bad \\.?bar"}} \
313 "pr12760.exe" "c"] \
314 [list "PR ld/12975" \
315 "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib -Wl,-version-script,pr12975.t" "-O2 -flto" \
316 {pr12975.c} {{"readelf" {-s --wide} "pr12975.d"}} "pr12975.so" "c"] \
317 [list "PR ld/13229" \
318 "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -finline -fno-early-inlining -flto" \
319 {pr13229.cc} {{"readelf" {-s --wide} "pr13229.d"}} "pr13229.so" "c++"] \
320 [list "PR ld/13244" \
321 "-shared -O2 -fPIC -flto -fuse-linker-plugin -nostdlib" "-O2 -fno-early-inlining -flto" \
322 {pr13244.c} {{"readelf" {-s --wide} "pr13244.d"}} "pr13244.so" "c"] \
323 [list "Build libpr16746a.a" \
324 "" "" \
325 {pr16746a.c pr16746b.c} {} "lib15146a.a"] \
326 [list "Build libpr16746b.a" \
327 "$plug_opt" "-O2 -flto" \
328 {pr16746c.c pr16746d.c} {} "lib15146b.a"] \
329 [list "PR ld/16746 (1)" \
330 "-O2 -flto -fuse-linker-plugin tmpdir/pr16746a.o tmpdir/pr16746c.o" "-O2 -flto" \
331 {dummy.c} {} "pr16746a.exe"] \
332 [list "PR ld/16746 (2)" \
333 "-O2 -flto -fuse-linker-plugin tmpdir/pr16746c.o tmpdir/pr16746a.o" "-O2 -flto" \
334 {dummy.c} {} "pr16746b.exe"] \
335 [list "Build pr21382a.o" \
336 "" "-O2 -flto" \
337 {pr21382a.c} {} "" "c"] \
338 [list "Build pr21382.so" \
339 "-shared" "-O2 -fpic" \
340 {pr21382b.c} {} "pr21382.so" "c"] \
341 [list {Build pr22220lib.so} \
342 {-shared -Wl,--version-script=pr22220lib.ver} {-fPIC} \
343 {pr22220lib.cc} {} {pr22220lib.so} {c++}] \
344 [list {Build pr22220main.o} \
345 {} {-flto} \
346 {pr22220main.cc} {} {} {c++}] \
347 [list "Build libpr23818.so" \
348 "-shared -flto -g -Wl,-version-script,pr23818.t" \
349 "-g -flto $lto_fat" \
350 {pr23818a.c pr23818b.c} \
351 {{"readelf" {-s --wide} "pr23818.d"}} \
352 "libpr23818.so"] \
353 [list "Build libpr23958.so" \
354 "-shared -flto -Wl,-version-script,pr23958.t" \
355 "-g -flto $lto_fat" \
356 {pr23958.c} \
357 "" \
358 "libpr23958.so"] \
359 [list "Build pr24486a.o" \
360 "$plug_opt" "-flto -O2" \
361 {pr24486a.c} {} "" "c"] \
362 [list "Build pr24486b.so" \
363 "-shared" "-O2 -fpic" \
364 {pr24486b.c} {} "pr24486b.so" "c"] \
365 [list "Build pr24486c.so" \
366 "-shared -Wl,--no-as-needed tmpdir/pr24486b.so" "-O2 -fpic" \
367 {pr24486c.c} {} "pr24486c.so" "c"] \
368 [list "PR ld/24486" \
369 "-O2 -flto tmpdir/pr24486a.o tmpdir/pr24486c.so -Wl,--as-needed tmpdir/pr24486b.so" "" \
370 {dummy.c} {} "pr24486.exe"] \
371 [list "Build pr25593a-1.o" \
372 "$plug_opt" "-flto -O2" \
373 {pr25593a-1.c} {} "" "c"] \
374 [list "Build pr25593a-2.o" \
375 "" "-O2" \
376 {pr25593a-2.c} {} "" "c"] \
377 [list "Build pr25593b.o" \
378 "" "-O2" \
379 {pr25593b.c} {} "" "c"] \
380 [list "Build pr25593c.so" \
381 "-shared" "-O2 -fpic" \
382 {pr25593c.c} {} "pr25593c.so" "c"] \
383 [list "Build pr25593d.so" \
384 "-shared" "-O2 -fpic" \
385 {pr25593d.c} {} "pr25593d.so" "c"] \
386 [list "Build pr25593e.so" \
387 "-shared" "-O2 -fpic" \
388 {pr25593e.c} {} "pr25593e.so" "c"] \
389 [list "PR ld/r25593 (LTO)" \
390 "-O2 -flto -Wl,--as-needed tmpdir/pr25593a-1.o tmpdir/pr25593b.o \
391 tmpdir/pr25593c.so tmpdir/pr25593d.so tmpdir/pr25593e.so" "" \
392 {dummy.c} {{readelf {-d --wide} pr25593.d}} "pr25593a.exe"] \
393 [list "PR ld/r25593" \
394 "-O2 -flto -Wl,--as-needed tmpdir/pr25593a-2.o tmpdir/pr25593b.o \
395 tmpdir/pr25593c.so tmpdir/pr25593d.so tmpdir/pr25593e.so" "" \
396 {dummy.c} {{readelf {-d --wide} pr25593.d}} "pr25593b.exe"] \
397 [list "Build pr25618a.o" \
398 "$plug_opt" "-O2 -flto -fpic" \
399 {pr25618a.cc} {} "" "c++"] \
400 [list "Build pr25618.a" \
401 "$plug_opt" "-O2 -flto -fpic" \
402 {pr25618b.cc} {} "pr25618.a" "c++"] \
403 [list "Build pr25618.so" \
404 "-shared -Wl,--exclude-libs,ALL tmpdir/pr25618a.o tmpdir/pr25618.a" \
405 "-fpic" \
406 {dummy.c} {{readelf {--dyn-syms --wide} pr25618.d}} "pr25618.so" "c++"] \
407 ]
408
409 # PR 14918 checks that libgcc is not spuriously included in a shared link of
410 # an empty program. The ARM crt1.o startup code however calls __libc_csu_init
411 # in /usr/lib/libc_nonshared.a(elf-init.oS). This in turn needs
412 # __aeabi_unwind_cpp_pr0@@GCC_3.5 which is provided by libgcc_s.so.1, so the
413 # test fails. Hence this code to skip the test.
414 if { ! [istarget "arm*-*-*"] } {
415 set lto_link_elf_tests [concat $lto_link_elf_tests [list \
416 [list "PR ld/14918" \
417 "-flto" "-flto" \
418 {pr14918.c} {{"readelf" {-d --wide} "pr14918.d"}} "pr14918.exe" "c"] \
419 ]]
420 }
421
422 # PR 12982 checks that an executable stack is not created by default
423 # when using the LTO plugin. The HPPA target however requires an
424 # executable stack for syscall restarts and signal returns, so we
425 # skip this test for that target.
426 if { ! [istarget "hppa*-*-*"] } {
427 set lto_link_elf_tests [concat $lto_link_elf_tests [list \
428 [list "PR ld/12982" \
429 "-O2 -flto -fuse-linker-plugin" "-O2 -flto" \
430 {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"] \
431 ]]
432 }
433
434 # Check final symbols in executables.
435 set lto_link_symbol_tests [list \
436 [list "LTO 3 symbol" \
437 "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" "" \
438 {dummy.c} {{"nm" {} "lto-3.d"}} "lto-3.exe" "c"] \
439 [list "LTO 5 symbol" \
440 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" "" \
441 {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"] \
442 [list "LTO 9 symbol" \
443 "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" "" \
444 {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"] \
445 [list "LTO 16a symbol" \
446 "-O2 $NOSANITIZE_CFLAGS -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" \
447 "-flto $NOSANITIZE_CFLAGS" \
448 {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"] \
449 [list "LTO 16b symbol" \
450 "-O2 $NOSANITIZE_CFLAGS -Wl,-e,foo -u bar -nostdlib -flto -fuse-linker-plugin" \
451 "-flto $NOSANITIZE_CFLAGS" \
452 {lto-16a.c lto-16b.c} {{"nm" {} "lto-16b.d"}} "lto-16b.exe" "c"] \
453 [list "PR ld/13183" \
454 "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" "" \
455 {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"] \
456 ]
457
458 # LTO run-time tests.
459 set lto_run_tests [list \
460 [list "LTO 3a" \
461 "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/liblto-3.a" "" \
462 {dummy.c} "lto-3b.exe" "lto-3.out" "" "c"] \
463 [list "LTO 3b" \
464 "-O2 -flto -fuse-linker-plugin tmpdir/lto-3a.o tmpdir/lto-3c.o tmpdir/lto-3.o" "" \
465 {dummy.c} "lto-3c.exe" "lto-3.out" "" "c"] \
466 [list "LTO 3c" \
467 "-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" "" \
468 {dummy.c} "lto-3d.exe" "lto-3.out" "" "c"] \
469 [list "LTO 5" \
470 "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" "" \
471 {dummy.c} "lto-5.exe" "lto-5.out" "" "c"] \
472 [list "LTO 11" \
473 "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" "" \
474 {dummy.c} "lto-11.exe" "lto-11.out" "" "c"] \
475 [list "LTO 12a" \
476 "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/liblto-12.a tmpdir/lto-12b.o" "" \
477 {dummy.c} "lto-12a.exe" "lto-12.out" "" "c"] \
478 [list "LTO 12b" \
479 "-O -flto -fuse-linker-plugin tmpdir/lto-12a.o tmpdir/lto-12b.o tmpdir/liblto-12.a" "" \
480 {dummy.c} "lto-12b.exe" "lto-12.out" "" "c"] \
481 [list "LTO 13" \
482 "-O -flto -fuse-linker-plugin tmpdir/lto-13a.o tmpdir/liblto-13.a tmpdir/lto-13b.o" "" \
483 {dummy.c} "lto-13.exe" "lto-13.out" "" "c"] \
484 [list "LTO 14" \
485 "-O2 -flto -fuse-linker-plugin tmpdir/lto-14a.o -Wl,--whole-archive tmpdir/liblto-14.a -Wl,--no-whole-archive tmpdir/lto-14b.o" "" \
486 {dummy.c} "lto-14.exe" "lto-14.out" "" "c"] \
487 [list "LTO 15" \
488 "-O2 -flto -fuse-linker-plugin -Wl,--start-group tmpdir/liblto-15.a tmpdir/lto-15a.o -Wl,--end-group" "" \
489 {dummy.c} "lto-15.exe" "lto-15.out" "" "c"] \
490 [list "PR ld/13066" \
491 "-O2 -flto -fuse-linker-plugin" "" \
492 {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"] \
493 [list "PR ld/13201" \
494 "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" "" \
495 {dummy.c} "pr13201.exe" "pr13201.out" "" "c"] \
496 [list "PR ld/15323 (3)" \
497 "-O2 -flto -fuse-linker-plugin tmpdir/pr15323a.o" "" \
498 {pr15323b.c} "pr15323.exe" "pr15323.out" "-flto -O2" "c"] \
499 [list "PR ld/15323 (4)" \
500 "-O2 -flto tmpdir/pr15323a-r.o" "" \
501 {dummy.c} "pr15323a.exe" "pr15323.out" "-flto -O2" "c"] \
502 [list "PR ld/19317 (3)" \
503 "-O2 -flto tmpdir/pr19317-r.o" "" \
504 {dummy.c} "pr19317.exe" "pr19317.out" "-flto -O2" "c"] \
505 [list "Run pr20276" \
506 "-O2 -flto tmpdir/pr20276a.o tmpdir/pr20276b.o" "" \
507 {dummy.c} "pr20276" "pass.out" "-flto -O2" "c"] \
508 [list "Run pr20267a" \
509 "-O2 -flto -fcommon tmpdir/pr20267a.o tmpdir/libpr20267a.a" "" \
510 {dummy.c} "pr20267a" "pass.out" "-flto -O2 -fcommon" "c"] \
511 [list "Run pr20267b" \
512 "-O2 -flto -fcommon tmpdir/pr20267a.o tmpdir/libpr20267b.a" "" \
513 {dummy.c} "pr20267b" "pass.out" "-flto -O2 -fcommon" "c"] \
514 [list "Run pr22502" \
515 "-O2 -flto tmpdir/pr22502a.o tmpdir/pr22502b.o" "" \
516 {dummy.c} "pr20267" "pass.out" "-flto -O2 -fcommon" "c"] \
517 [list "Run pr22751" \
518 "-O2 -flto" "" \
519 {dummy.c} "pr22751" "pass.out" "-flto -O2" "c" "" \
520 "-Wl,--whole-archive tmpdir/pr22751.a -Wl,--no-whole-archive"] \
521 [list "Run pr24406-1" \
522 "-O2 -flto" "" \
523 {pr24406-1.c} "pr24406-1" "pass.out" "-flto -O2" "c" "" \
524 "-Wl,--wrap=read"] \
525 [list "Run pr24406-2" \
526 "-O2 -flto" "" \
527 {pr24406-2a.c} "pr24406-2" "pass.out" \
528 "-flto -O2" "c" "" \
529 "tmpdir/pr24406-2b.o -Wl,--wrap=cook"] \
530 [list "Run pr26163" \
531 "-O2 -flto" "" \
532 {pr26163b.c} "pr24406-2" "pass.out" \
533 "-flto -O2" "c" "" \
534 "tmpdir/pr26163a.o -Wl,--defsym,g=real_g"] \
535 [list "Run pr26262a" \
536 "-O2 -flto" "" \
537 {pr26262a.c} "pr26262a" "pass.out" \
538 "-flto -O2" "c" "" \
539 "tmpdir/pr26262b.o tmpdir/pr26262c.o"] \
540 [list "Run pr26262b" \
541 "-flto -O2 tmpdir/pr26262b.o tmpdir/pr26262c.o" "" \
542 {pr26262a.c} "pr26262b" "pass.out" \
543 "-flto -O2" "c" "" \
544 ""] \
545 ]
546
547 if { [at_least_gcc_version 4 7] } {
548 set lto_run_tests [concat $lto_run_tests [list \
549 [list "PR ld/12942 (1)" \
550 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" "" \
551 {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"] \
552 [list "PR ld/12942 (2)" \
553 "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" "" \
554 {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"] \
555 ]]
556 }
557
558 # LTO run-time tests for ELF which require shared library support.
559 set lto_run_elf_shared_tests [list \
560 [list "LTO 7" \
561 "-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" "" \
562 {dummy.c} "lto-7.exe" "lto-7.out" "" "c"] \
563 [list "Run pr21382" \
564 "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr21382a.o tmpdir/pr21382.so" "" \
565 {dummy.c} "pr21382.exe" "pass.out" "" "c"] \
566 [list {pr22220a} \
567 {-flto -fuse-linker-plugin tmpdir/pr22220main.o tmpdir/pr22220lib.so} {} \
568 {dummy.c} {pr22220a.exe} {pass.out} {} {c++}] \
569 [list {pr22220b} \
570 {-flto -fuse-linker-plugin -Wl,--no-as-needed tmpdir/pr22220lib.so tmpdir/pr22220main.o} {} \
571 {dummy.c} {pr22220b.exe} {pass.out} {} {c++}] \
572 ]
573
574 # LTO run-time tests for ELF
575 set lto_run_elf_tests [list \
576 [list "LTO 8" \
577 "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" "" \
578 {dummy.c} "lto-8.exe" "lto-8.out" "" "c"] \
579 [list "LTO TLS IE" \
580 "-O2 -flto -fuse-linker-plugin" "" \
581 {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"] \
582 ]
583
584 run_cc_link_tests $lto_link_tests
585
586 # These compilation tests generate intermediate object files which will be used
587 # by some elf tests besides shared libs tests. So, always compile them.
588 run_cc_link_tests $lto_compile_elf_tests
589
590 # Restrict these to ELF targets that support shared libs and PIC.
591 if { [is_elf_format] && [check_lto_shared_available] } {
592 run_cc_link_tests $lto_link_elf_tests
593 set testname "PR ld/16746 (3)"
594 set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746b.o tmpdir/pr16746d.o"]
595 if { [ regexp "warning: \\.?foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
596 pass $testname
597 } {
598 fail $testname
599 }
600 set testname "PR ld/16746 (4)"
601 set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746d.o tmpdir/pr16746b.o"]
602 if { [ regexp "warning: \\.?foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
603 pass $testname
604 } {
605 fail $testname
606 }
607 }
608
609 set testname "Build liblto-11.a"
610 remote_file host delete "tmpdir/liblto-11.a"
611 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"]
612 if {![string match "" $catch_output]} {
613 unresolved $testname
614 restore_notify
615 return
616 }
617
618 if { [at_least_gcc_version 4 7] } {
619 # Check expected LTO linker errors.
620 # Since the asm symbol name hack in pr12365b.c doesn't work on all
621 # targets, run PR ld/12365 tests only for known targets.
622 if { ([istarget "i?86-*-elf*"]
623 || [istarget "i?86-*-linux*"]
624 || [istarget "i?86-*-gnu*"]
625 || [istarget "x86_64-*-linux*"]
626 || [istarget "amd64-*-linux*"]) } {
627 set testname "PR ld/12365"
628 set exec_output [run_host_cmd "$CC" "-O2 -flto -flto-partition=none -fuse-linker-plugin -o tmpdir/pr12365 tmpdir/pr12365a.o tmpdir/pr12365b.o tmpdir/pr12365c.o"]
629 set exec_output [prune_warnings $exec_output]
630 if { [ regexp "undefined reference to `my_bcopy'" $exec_output ] } {
631 # Linker should catch the reference to undefined `my_bcopy'
632 # error caused by a GCC bug.
633 pass $testname
634 } elseif { [ string match "" $exec_output ] } {
635 global READELF
636 set exec_output [run_host_cmd "$READELF" "-s -W tmpdir/pr12365"]
637 if { [ regexp "my_bcopy" $exec_output ] } {
638 # Verify that there is no `my_bcopy' symbol in executable.
639 fail $testname
640 } {
641 pass $testname
642 }
643 } {
644 fail $testname
645 }
646
647 run_cc_link_tests [list \
648 [list \
649 "Build pr22721a.so" \
650 "-shared $NOSANITIZE_CFLAGS -nostdlib -nostartfiles \
651 -Wl,-version-script,pr22721.t" \
652 "" \
653 {pr22721a.s} \
654 {} \
655 "pr22721a.so" \
656 ] \
657 [list \
658 "Build pr22721b.o" \
659 "$plug_opt $NOSANITIZE_CFLAGS" \
660 "-O2 -fPIC -flto $lto_no_fat" \
661 {pr22721b.c} \
662 ] \
663 [list \
664 "Build PR ld/pr22721" \
665 "-O2 -flto -fuse-linker-plugin -nostdlib -nostartfiles \
666 $NOSANITIZE_CFLAGS \
667 -Wl,-e,_start tmpdir/pr22721b.o tmpdir/pr22721a.so" \
668 "$NOSANITIZE_CFLAGS" \
669 {dummy.c} \
670 {} \
671 "pr22721.exe"
672 ] \
673 ]
674 }
675 set testname "PR ld/12942 (3)"
676 set exec_output [run_host_cmd "$CXX" "-O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
677 if { [ regexp "undefined reference to `\\.?link_error\\(\\)'" $exec_output ] } {
678 pass $testname
679 } {
680 fail $testname
681 }
682
683 run_cc_link_tests [list \
684 [list \
685 "Build pr23460*.o" \
686 "$plug_opt" \
687 "-O2 -fPIC -flto $lto_no_fat" \
688 {pr23460a.c pr23460b.c pr23460c.c \
689 pr23460d.c pr23460e.c pr23460f.c} \
690 ] \
691 ]
692 set exec_output [run_host_cmd "sh" \
693 "-c \"ulimit -n 16; \
694 $ar -rc $plug_opt \
695 tmpdir/libpr23460.a \
696 tmpdir/pr23460a.o \
697 tmpdir/pr23460b.o \
698 tmpdir/pr23460c.o \
699 tmpdir/pr23460d.o \
700 tmpdir/pr23460e.o \
701 tmpdir/pr23460f.o\""]
702 set exec_output [prune_warnings $exec_output]
703 if [string match "" $exec_output] then {
704 pass "PR binutils/23460"
705 } else {
706 fail "PR binutils/23460"
707 }
708 }
709
710 # Run "ld -r" to generate inputs for complex LTO tests.
711 run_dump_test "lto-3r"
712 remote_exec host "mv" "tmpdir/dump tmpdir/lto-3.o"
713 run_dump_test "lto-5r"
714 remote_exec host "mv" "tmpdir/dump tmpdir/lto-5.o"
715
716 run_cc_link_tests $lto_link_symbol_tests
717
718 run_ld_link_tests [list \
719 [list "PR ld/19317 (2)" \
720 "-r tmpdir/pr19317.o" "" "" \
721 {dummy.s} {} "pr19317-r.o"] \
722 ]
723
724 run_ld_link_exec_tests $lto_run_tests
725
726 if { [is_elf_format] } {
727 run_ld_link_exec_tests $lto_run_elf_tests
728
729 # Note - it is not guaranteed that the ordering of symbols in the dynamic
730 # symbol table will match the ordering of the symbols specified by the
731 # --dynamic-list command line option.
732 #
733 # For PR22983 we want to make sure that all four symbols specified in
734 # pr222983.t are present in the output, but a simple sequences of regexps
735 # will not work as we do not know the order of the symbols. (Readelf
736 # does not have a symbol sorting option and the run_cc_list_tests proc
737 # does not allow for the output of the dump program to piped into `sort`).
738 #
739 # So instead we run readelf four times, each time checking for the
740 # presence of a specific symbol from the pr22983.t file.
741 run_cc_link_tests [list \
742 [list \
743 "Build pr22983" \
744 "-Wl,--dynamic-list,pr22983.t" \
745 "-flto" \
746 {pr22983a.c pr22983b.c} \
747 {{readelf {--dyn-syms --wide} pr22983.1.d} \
748 {readelf {--dyn-syms --wide} pr22983.2.d} \
749 {readelf {--dyn-syms --wide} pr22983.3.d} \
750 {readelf {--dyn-syms --wide} pr22983.4.d}} \
751 "pr22983" \
752 ] \
753 ]
754 }
755
756 if { [is_elf_format] && [check_lto_shared_available] } {
757 run_ld_link_exec_tests $lto_run_elf_shared_tests
758 }
759
760 proc pr20103 {cflags libs} {
761 global CC
762
763 set testname "PR ld/20103 ($cflags $libs)"
764 set exec_output [run_host_cmd "$CC" "$cflags $libs"]
765 if { [ regexp "undefined reference to `\\.?dead'" $exec_output ] } {
766 pass "$testname (1)"
767 } {
768 fail "$testname (1)"
769 }
770 if { [ regexp "plugin needed to handle lto object" $exec_output ] } {
771 fail "$testname (2)"
772 } {
773 pass "$testname (2)"
774 }
775 }
776
777 if { [check_lto_fat_available] } {
778 run_cc_link_tests [list \
779 [list \
780 "Build fatpr20103a.a" \
781 "$plug_opt" "-flto -ffat-lto-objects" \
782 {pr20103a.c} {} "fatpr20103a.a"
783 ] \
784 [list \
785 "Build fatpr20103b.a" \
786 "$plug_opt" "-flto -ffat-lto-objects" \
787 {pr20103b.c} {} "fatpr20103b.a"
788 ] \
789 [list \
790 "Build fatpr20103c.a" \
791 "$plug_opt" "-flto -ffat-lto-objects" \
792 {pr20103c.c} {} "fatpr20103c.a" \
793 ] \
794 [list \
795 "Build thinpr20103a.a" \
796 "$plug_opt" "-flto -fno-fat-lto-objects" \
797 {pr20103a.c} {} "thinpr20103a.a"
798 ] \
799 [list \
800 "Build thinpr20103b.a" \
801 "$plug_opt" "-flto -fno-fat-lto-objects" \
802 {pr20103b.c} {} "thinpr20103b.a"
803 ] \
804 [list \
805 "Build thinpr20103c.a" \
806 "$plug_opt" "-flto -fno-fat-lto-objects" \
807 {pr20103c.c} {} "thinpr20103c.a" \
808 ] \
809 [list \
810 "Build pr20103a" \
811 "-O2 -flto -Wl,--start-group tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a -Wl,--end-group" \
812 "-O2 -flto" \
813 {dummy.c} {} "pr20103a" \
814 ] \
815 [list \
816 "Build pr20103b" \
817 "-O2 -flto -Wl,--start-group tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a -Wl,--end-group" \
818 "-O2 -flto" \
819 {dummy.c} {} "pr20103b" \
820 ] \
821 [list \
822 "Build pr20103c" \
823 "-O2 -Wl,--start-group tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a -Wl,--end-group" \
824 "-O2" \
825 {dummy.c} {} "pr20103c" \
826 ] \
827 ]
828 pr20103 "-O2 -flto" "tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a"
829 pr20103 "-O2 -flto" "tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a"
830 pr20103 "-O2" "tmpdir/fatpr20103a.a tmpdir/fatpr20103b.a tmpdir/fatpr20103c.a"
831
832 if { [at_least_gcc_version 4 9] } {
833 run_cc_link_tests [list \
834 [list \
835 "Build pr20103d" \
836 "-O2 -Wl,--start-group tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a -Wl,--end-group" \
837 "-O2" \
838 {dummy.c} {} "pr20103d" \
839 ] \
840 ]
841 pr20103 "-O2" "tmpdir/thinpr20103a.a tmpdir/thinpr20103b.a tmpdir/thinpr20103c.a"
842 }
843 }
844
845 restore_notify