Don't add DT_NEEDED for references from the dummy bfd
[external/binutils.git] / ld / testsuite / ld-plugin / lto.exp
1 # Expect script for ld-plugin LTO tests
2 #   Copyright 2011
3 #   Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Binutils.
6 #
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.
11 #
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.
16 #
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,
20 # MA 02110-1301, USA.
21
22 # These tests require plugin and LTO.
23 if { ![check_plugin_api_available]
24      || ![check_lto_available] } {
25     return
26 }
27
28 global CFLAGS
29 global CXXFLAGS
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
34
35 proc restore_notify { } {
36   global saved_CFLAGS
37   global saved_CXXFLAGS
38   set CFLAGS "$saved_CFLAGS"
39   set CXXFLAGS "$saved_CXXFLAGS"
40 }
41
42 # Simple LTO tests and generate input files for complex LTO tests.
43 set lto_link_tests {
44   {"LTO 1"
45    "-O2 -flto -fuse-linker-plugin" "-flto -fuse-linker-plugin"
46    {lto-1a.c lto-1b.c} {} "lto-1.exe"}
47   {"Build libdummy.a 2"
48    "" "-O2 -flto -fuse-linker-plugin"
49    {lto-2.c} {} "libdummy.a"}
50   {"LTO 2"
51    "-static -O2 -flto -fuse-linker-plugin tmpdir/lto-2.o -lm" ""
52    {dummy.c} {} "lto-2.exe"}
53   {"Build libdummy.a 3a"
54    "" "-flto"
55    {lto-3a.c} {} "libdummy.a"}
56   {"Build libdummy.a 3c"
57    "" "-O2"
58    {lto-3c.c} {} "libdummy.a"}
59   {"Build liblto-3.a"
60    "" "-flto"
61    {lto-3b.c} {} "liblto-3.a"}
62   {"Build libdummy.a 5a"
63    "" "-flto"
64    {lto-5a.c} {} "libdummy.a"}
65   {"Build libdummy.a 5b"
66    "" "-flto"
67    {lto-5b.c} {} "libdummy.a"}
68   {"LTO 6"
69    "-O2 -flto -fuse-linker-plugin" ""
70    {lto-6.c} {} "lto-6.exe" "c"}
71   {"Build libdummy.a 9"
72    "" "-O2 -finline -flto"
73    {lto-9.cc} {} "libdummy.a"}
74   {"Build libdummy.a 11a"
75    "" "-O -flto"
76    {lto-11a.c} {} "libdummy.a"}
77   {"Build libdummy.a 11b"
78    "" "-O -flto"
79    {lto-11b.c} {} "libdummy.a"}
80   {"Build libdummy.a 11c"
81    "" "-O"
82    {lto-11c.c} {} "libdummy.a"}
83   {"Build liblto-12.a"
84    "" "-O2 -flto"
85    {lto-12c.c} {} "liblto-12.a"}
86   {"Build libdummy.a 12"
87    "" "-O2 -flto"
88    {lto-12a.c lto-12b.c} {} "libdummy.a"}
89   {"Build libdummy.a 13"
90    "" "-O2 -flto"
91    {lto-13a.c lto-13b.c} {} "libdummy.a"}
92   {"Build liblto-13.a"
93    "" "-O2"
94    {lto-13c.c} {} "liblto-13.a"}
95   {"Build libdummy.a 14a"
96    "" "-flto"
97    {lto-14a.c lto-14b.c} {} "libdummy.a"}
98   {"Build liblto-14.a"
99    "" "-flto"
100    {lto-14c.c} {} "liblto-14.a"}
101   {"Build libdummy.a 15a"
102    "" "-flto"
103    {lto-15a.c} {} "libdummy.a"}
104   {"Build liblto-15.a"
105    "" "-flto"
106    {lto-15b.c} {} "liblto-15.a"}
107   {"PR ld/12696"
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"
111    "" ""
112    {pr12758a.s} {} "libdummy.a"}
113   {"Build libpr12758.a"
114    "" "-flto -O2"
115    {pr12758b.c} {} "libpr12758.a"}
116   {"PR ld/12758"
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"
120    "" ""
121    {pr12760a.c} {} "libdummy.a"}
122   {"Build libpr12760.a"
123    "" "-flto -O2"
124    {pr12760b.c} {} "libpr12760.a"}
125   {"PR ld/12760"
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)"
129    "" "-flto -O2"
130    {pr12942a.cc pr12942c.cc} {} "libdummy.a" "c++"}
131   {"Build libdummy.a PR ld/12942 (2)"
132    "" "-O0"
133    {pr12942b.cc} {} "libdummy.a" "c++"}
134   {"Build libpr13183.a"
135    "-T" "-flto -O2"
136    {pr13183a.c} {} "libpr13183.a"}
137   {"Build libdummy.a PR ld/13183"
138    "" "-flto -O2"
139    {pr13183b.c} {} "libdummy.a"}
140   {"Build libdummy.a PR ld/13201"
141    "" "-flto -O2"
142    {pr13201.c} {} "libdummy.a"}
143   {"PR ld/13287"
144    "-flto -fuse-linker-plugin -Wl,--as-needed" "-flto"
145    {pr13287.cc} {} "pr13287.exe" "c++"}
146 }
147
148 # Generate input files for complex LTO tests for ELF.
149 set lto_link_elf_tests {
150   {"Build libdummy.a 7"
151    "" "-flto -O2"
152    {lto-7a.c lto-7b.c lto-7c.c} {} "libdummy.a"}
153   {"Build liblto-7.so"
154    "-shared" "-O2 -fpic"
155    {lto-7d.c} {} "liblto-7.so" "c"}
156   {"Build libdummy.a 8a"
157    "" "-O2"
158    {lto-8a.c} {} "libdummy.a"}
159   {"Build libdummy.a 8b"
160    "" "-flto -O2"
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"}
171   {"PR ld/12982"
172    "-O2 -flto -fuse-linker-plugin" "-O2 -flto"
173    {pr12982.c} {{"readelf" {-l --wide} "pr12982.d"}} "pr12982.exe"}
174   {"PR ld/12975"
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"}
177   {"PR ld/13229"
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++"}
180   {"PR ld/13244"
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"
184    "" "-flto -O2"
185    {pr15146a.c} {} "lib15146a.a"}
186   {"Build pr15146b.so"
187    "-shared" "-O2 -fpic"
188    {pr15146b.c} {} "pr15146b.so" "c"}
189   {"Build pr15146c.so"
190    "-shared tmpdir/pr15146b.so" "-O2 -fpic"
191    {pr15146c.c} {} "pr15146c.so" "c"}
192   {"PR ld/15146 (1)"
193    "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries tmpdir/pr15146a.o tmpdir/pr15146c.so" ""
194    {dummy.c} {{"readelf" {-d} "pr15146.d"}} "pr15146a.exe"}
195   {"Build libpr15146d.a"
196    "" "-flto -O2"
197    {pr15146d.c} {} "lib15146d.a"}
198   {"Build libpr15146d.a"
199    "" "-flto -O2"
200    {pr15146d.c} {} "lib15146d.a"}
201 }
202
203 # Check final symbols in executables.
204 set lto_link_symbol_tests {
205   {"LTO 3 symbol"
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"}
208   {"LTO 5 symbol"
209    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
210    {dummy.c} {{"nm" {} "lto-5.d"}} "lto-5.exe" "c"}
211   {"LTO 9 symbol"
212    "-O2 -flto -fuse-linker-plugin tmpdir/lto-9.o" ""
213    {dummy.c} {{"nm" {-C} "lto-9.d"}} "lto-9.exe" "c++"}
214   {"LTO 16a symbol"
215    "-O2 -Wl,-e,foo -nostdlib -flto -fuse-linker-plugin" "-flto"
216    {lto-16a.c} {{"nm" {} "lto-16a.d"}} "lto-16.exe" "c"}
217   {"LTO 16b symbol"
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"}
220   {"PR ld/13183"
221    "-O2 -flto -fuse-linker-plugin tmpdir/pr13183b.o tmpdir/libpr13183.a" ""
222    {dummy.c} {{"nm" {} "pr13183.d"}} "pr13183.exe" "c"}
223 }
224
225 # LTO run-time tests.
226 set lto_run_tests {
227   {"LTO 3a"
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"}
230   {"LTO 3b"
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"}
233   {"LTO 3c"
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"}
236   {"LTO 5"
237    "-O2 -flto -fuse-linker-plugin tmpdir/lto-5.o" ""
238    {dummy.c} "lto-5.exe" "lto-5.out" "" "c"}
239   {"LTO 11"
240    "-O -flto -fuse-linker-plugin tmpdir/liblto-11.a" ""
241    {dummy.c} "lto-11.exe" "lto-11.out" "" "c"}
242   {"LTO 12a"
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"}
245   {"LTO 12b"
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"}
248   {"LTO 13"
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"}
251   {"LTO 14"
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"}
254   {"LTO 15"
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"}
257   {"PR ld/12942 (1)"
258    "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942b.o" ""
259    {dummy.c} "pr12942a.exe" "pr12942.out" "" "c++"}
260   {"PR ld/12942 (2)"
261    "-O2 -flto -fuse-linker-plugin tmpdir/pr12942a.o tmpdir/pr12942c.o" ""
262    {dummy.c} "pr12942c.exe" "pr12942.out" "" "c++"}
263   {"PR ld/13066"
264    "-O2 -flto -fuse-linker-plugin" ""
265    {pr13066.cc} "pr13066.exe" "pr13066.out" "" "c++"}
266   {"PR ld/13201"
267    "-O2 -flto -fuse-linker-plugin -Wl,--as-needed tmpdir/pr13201.o -lm" ""
268    {dummy.c} "pr13201.exe" "pr13201.out" "" "c"}
269 }
270
271 # LTO run-time tests for ELF
272 set lto_run_elf_tests {
273   {"LTO 7"
274    "-O2 -flto -fuse-linker-plugin tmpdir/lto-7b.o tmpdir/lto-7c.o tmpdir/lto-7a.o tmpdir/liblto-7.so" ""
275    {dummy.c} "lto-7.exe" "lto-7.out" "" "c"}
276   {"LTO 8"
277    "-O2 -flto -fuse-linker-plugin tmpdir/lto-8b.o tmpdir/lto-8a.o" ""
278    {dummy.c} "lto-8.exe" "lto-8.out" "" "c"}
279   {"LTO TLS IE"
280    "-O2 -flto -fuse-linker-plugin" ""
281    {run-ie.c} "run-ie.exe" "run-ie.out" "" "c"}
282 }
283
284 run_cc_link_tests $lto_link_tests
285
286 # Restrict these to ELF targets that support shared libs and PIC.
287 if { [is_elf_format]
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 tmpdir/pr15146d.o tmpdir/pr15146c.so"]
292     if { [ regexp "ltrans.o: undefined reference to symbol 'xxx'" $exec_output ] } {
293         pass $testname
294     } {
295         fail $testname
296     }
297 }
298
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]} {
303     unresolved $testname
304     restore_notify
305     return
306 }
307
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 ] } {
312     pass $testname
313 } {
314     fail $testname
315 }
316
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"
322
323 run_cc_link_tests $lto_link_symbol_tests
324
325 # The following tests require running the executable generated by ld.
326 if ![isnative] {
327     return
328 }
329
330 run_ld_link_exec_tests [] $lto_run_tests
331
332 if { [is_elf_format] } {
333     run_ld_link_exec_tests [] $lto_run_elf_tests
334 }
335
336 restore_notify