ld-elfvers don't fail on non-empty results
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfweak / elfweak.exp
CommitLineData
16a57284 1# Expect script for ld-weak tests
6f2750fe 2# Copyright (C) 2001-2016 Free Software Foundation, Inc.
16a57284 3#
f96b4a7b
NC
4# This file is part of the GNU Binutils.
5#
6# This program is free software; you can redistribute it and/or modify
16a57284 7# it under the terms of the GNU General Public License as published by
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
16a57284 9# (at your option) any later version.
f96b4a7b 10#
16a57284
L
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.
f96b4a7b 15#
16a57284
L
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
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
16a57284
L
20#
21# Written by H.J. Lu (hjl@gnu.org)
22# Eric Youngdale (eric@andante.jic.com)
23#
24
25# This test can only be run if ld generates native executables.
26if ![isnative] then {return}
27
28# This test can only be run on a couple of ELF platforms.
29# Square bracket expressions seem to confuse istarget.
30# This is similar to the test that is used in ld-shared, BTW.
99753d9d
AM
31if { ![istarget alpha*-*-linux*]
32 && ![istarget arm*-*-linux*]
33 && ![istarget hppa*64*-*-hpux*]
34 && ![istarget hppa*-*-linux*]
35 && ![istarget i?86-*-sysv4*]
36 && ![istarget i?86-*-unixware]
37 && ![istarget i?86-*-elf*]
38 && ![istarget i?86-*-linux*]
39 && ![istarget i?86-*-gnu*]
40 && ![istarget ia64-*-elf*]
41 && ![istarget ia64-*-linux*]
42 && ![istarget m68k-*-linux*]
43 && ![istarget mips*-*-irix5*]
44 && ![istarget mips*-*-linux*]
45 && ![istarget powerpc*-*-elf*]
46 && ![istarget powerpc*-*-linux*]
47 && ![istarget powerpc*-*-sysv4*]
48 && ![istarget sh\[34\]*-*-linux*]
49 && ![istarget sparc*-*-elf]
50 && ![istarget sparc*-*-solaris2*]
5a68afcf 51 && ![istarget sparc*-*-linux*]
75c8d84d 52 && ![istarget x86_64-*-linux*]
5a68afcf 53 && ![istarget *-*-nacl*] } {
16a57284
L
54 return
55}
56
99753d9d
AM
57if { [istarget *-*-linux*aout*]
58 || [istarget *-*-linux*oldld*] } {
16a57284
L
59 return
60}
61
62if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
63 return
64}
65
66set diff diff
67set tmpdir tmpdir
68set DOBJDUMP_FLAGS --dynamic-syms
69set SOBJDUMP_FLAGS --syms
75c8d84d 70set shared "--shared -Wl,--no-as-needed"
16a57284 71
5940a93c 72
16a57284
L
73#
74# objdump_symstuff
75# Dump non-dynamic symbol stuff and make sure that it is sane.
76#
77proc objdump_symstuff { objdump object expectfile } {
78 global SOBJDUMP_FLAGS
79 global version_output
80 global diff
81 global tmpdir
82
83 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
84
85 verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out"
86
87 catch "exec $objdump $SOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out" exec_output
88 set exec_output [prune_warnings $exec_output]
89 if [string match "" $exec_output] then {
90
91# Now do a line-by-line comparison to effectively diff the darned things
92# The stuff coming from the expectfile is actually a regex, so we can
93# skip over the actual addresses and so forth. This is currently very
94# simpleminded - it expects a one-to-one correspondence in terms of line
95# numbers.
96
97 if [file exists $expectfile] then {
98 set file_a [open $expectfile r]
99 } else {
100 perror "$expectfile doesn't exist"
101 return 0
102 }
5a68afcf 103
16a57284
L
104 if [file exists $tmpdir/objdump.out] then {
105 set file_b [open $tmpdir/objdump.out r]
106 } else {
107 perror "$tmpdir/objdump.out doesn't exist"
108 return 0
109 }
110
111 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
112
113 set eof -1
114 set differences 0
115
116 while { [gets $file_a line] != $eof } {
117 if [regexp "^#.*$" $line] then {
118 continue
119 } else {
120 lappend list_a $line
121 }
122 }
123 close $file_a
124
125 while { [gets $file_b line] != $eof } {
126 if [regexp "^#.*$" $line] then {
127 continue
128 } else {
129 lappend list_b $line
130 }
131 }
132 close $file_b
5a68afcf 133
16a57284
L
134 for { set i 0 } { $i < [llength $list_a] } { incr i } {
135 set line_a [lindex $list_a $i]
136 set line_b [lindex $list_b $i]
5a68afcf 137
16a57284
L
138
139 verbose "\t$expectfile: $i: $line_a" 3
140 verbose "\t/tmp/objdump.out: $i: $line_b" 3
141 if [regexp $line_a $line_b] then {
142 continue
143 } else {
144 verbose -log "\t$expectfile: $i: $line_a"
145 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
5a68afcf 146
16a57284
L
147 return 0
148 }
149 }
5a68afcf 150
16a57284
L
151 if { [llength $list_a] != [llength $list_b] } {
152 verbose -log "Line count"
153 return 0
154 }
5a68afcf 155
16a57284
L
156 if $differences<1 then {
157 return 1
158 }
5a68afcf 159
16a57284
L
160 return 0
161 } else {
162 verbose -log "$exec_output"
163 return 0
164 }
165
166}
167
168#
169# objdump_dymsymstuff
170# Dump dynamic symbol stuff and make sure that it is sane.
171#
172proc objdump_dynsymstuff { objdump object expectfile } {
173 global DOBJDUMP_FLAGS
174 global version_output
175 global diff
176 global tmpdir
177
178 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
179
180 verbose -log "$objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out"
181
182 catch "exec $objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out" exec_output
183 set exec_output [prune_warnings $exec_output]
184 if [string match "" $exec_output] then {
185
186# Now do a line-by-line comparison to effectively diff the darned things
187# The stuff coming from the expectfile is actually a regex, so we can
188# skip over the actual addresses and so forth. This is currently very
189# simpleminded - it expects a one-to-one correspondence in terms of line
190# numbers.
191
192 if [file exists $expectfile] then {
193 set file_a [open $expectfile r]
194 } else {
195 warning "$expectfile doesn't exist"
196 return 0
197 }
5a68afcf 198
16a57284
L
199 if [file exists $tmpdir/objdump.out] then {
200 set file_b [open $tmpdir/objdump.out r]
201 } else {
202 fail "$tmpdir/objdump.out doesn't exist"
203 return 0
204 }
205
206 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
207
208 set eof -1
209 set differences 0
210
211 while { [gets $file_a line] != $eof } {
212 if [regexp "^#.*$" $line] then {
213 continue
214 } else {
215 lappend list_a $line
216 }
217 }
218 close $file_a
219
220 while { [gets $file_b line] != $eof } {
221 if [regexp "^#.*$" $line] then {
222 continue
223 } else {
224 lappend list_b $line
225 }
226 }
227 close $file_b
5a68afcf 228
16a57284
L
229 for { set i 0 } { $i < [llength $list_b] } { incr i } {
230 set line_b [lindex $list_b $i]
5a68afcf 231
16a57284
L
232# The tests are rigged so that we should never export a symbol with the
233# word 'hide' in it. Thus we just search for it, and bail if we find it.
234 if [regexp "hide" $line_b] then {
235 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
5a68afcf 236
16a57284
L
237 return 0
238 }
239
240 verbose "\t$expectfile: $i: $line_b" 3
241
242 # We can't assume that the sort is consistent across
243 # systems, so we must check each regexp. When we find a
244 # regexp, we null it out, so we don't match it twice.
245 for { set j 0 } { $j < [llength $list_a] } { incr j } {
246 set line_a [lindex $list_a $j]
247
248 if [regexp $line_a $line_b] then {
249 lreplace $list_a $j $j "CAN NOT MATCH"
250 break
251 }
252 }
253
254 if { $j >= [llength $list_a] } {
255 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
5a68afcf 256
16a57284
L
257 return 0
258 }
259 }
5a68afcf 260
16a57284
L
261 if { [llength $list_a] != [llength $list_b] } {
262 verbose -log "Line count"
263 return 0
264 }
5a68afcf 265
16a57284
L
266 if $differences<1 then {
267 return 1
268 }
5a68afcf 269
16a57284
L
270 return 0
271 } else {
272 verbose -log "$exec_output"
273 return 0
274 }
275
276}
277
bd7c9df6 278proc build_lib {test libname objs dynsymexp} {
b765d4e3 279 global CC
16a57284
L
280 global objdump
281 global tmpdir
282 global shared
283 global srcdir
284 global subdir
285
bd7c9df6
L
286 set files ""
287 foreach obj $objs {
288 set files "$files $tmpdir/$obj"
289 }
290
b765d4e3 291 if {![ld_simple_link $CC $tmpdir/$libname.so "$shared $files"]} {
16a57284
L
292 fail $test
293 return
294 }
295
99753d9d 296 if {![string match "" $dynsymexp]
a664545d 297 && ![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$dynsymexp]} {
16a57284
L
298 fail $test
299 return
300 }
301 pass $test
302}
303
bd7c9df6 304proc build_exec { test execname objs flags dat dynsymexp symexp} {
b765d4e3 305 global CC
16a57284
L
306 global objdump
307 global tmpdir
16a57284
L
308 global srcdir
309 global subdir
310 global exec_output
311
bd7c9df6
L
312 set files ""
313 foreach obj $objs {
314 set files "$files $tmpdir/$obj"
315 }
316
b765d4e3 317 if {![ld_simple_link $CC $tmpdir/$execname "$flags $files"]} {
16a57284
L
318 fail "$test"
319 return
320 }
321
322 if {![string match "" $dynsymexp]} then {
323 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$dynsymexp]} {
324 fail $test
325 return
326 }
327 }
328
329 if {![string match "" $symexp]} then {
330 if {![objdump_symstuff $objdump $tmpdir/$execname $srcdir/$subdir/$symexp]} {
331 fail $test
332 return
333 }
334 }
335
336 # Run the resulting program
337 send_log "$tmpdir/$execname >$tmpdir/$execname.out\n"
338 verbose "$tmpdir/$execname >$tmpdir/$execname.out"
339 catch "exec $tmpdir/$execname >$tmpdir/$execname.out" exec_output
340 if ![string match "" $exec_output] then {
341 send_log "$exec_output\n"
342 verbose "$exec_output"
343 fail $test
344 return
345 }
346
347 send_log "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat\n"
348 verbose "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat"
349 catch "exec diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat" exec_output
350 set exec_output [prune_warnings $exec_output]
351
352 if {![string match "" $exec_output]} then {
353 send_log "$exec_output\n"
354 verbose "$exec_output"
355 fail $test
356 return
357 }
358
359 pass $test
360}
361
fb35d3d8
DD
362# Old version of GCC for MIPS default to enabling -fpic
363# and get confused if it is used on the command line.
364if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
16a57284
L
365 set picflag ""
366} else {
367 # Unfortunately, the gcc argument is -fpic and the cc argument is
368 # -KPIC. We have to try both.
369 set picflag "-fpic"
370 send_log "$CC $picflag\n"
371 verbose "$CC $picflag"
372 catch "exec $CC $picflag" exec_output
373 send_log "$exec_output\n"
374 verbose "--" "$exec_output"
99753d9d
AM
375 if { [string match "*illegal option*" $exec_output]
376 || [string match "*option ignored*" $exec_output]
377 || [string match "*unrecognized option*" $exec_output]
16a57284
L
378 || [string match "*passed to ld*" $exec_output] } {
379 if [istarget *-*-sunos4*] {
380 set picflag "-pic"
381 } else {
382 set picflag "-KPIC"
383 }
384 }
385}
386verbose "Using $picflag to compile PIC code"
387
388if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o] {
389 unresolved "ELF weak"
390 return
391}
392
393if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o] {
394 unresolved "ELF weak"
395 return
396}
397
398if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main.c $tmpdir/main.o] {
399 unresolved "ELF weak"
400 return
401}
402
b765d4e3 403if {![ld_simple_link $CC $tmpdir/libbar.so "$shared $tmpdir/bar.o"]} {
bd7c9df6
L
404 fail "ELF weak"
405 return
406}
407
408if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o] {
409 unresolved "ELF weak"
410 return
411}
412
413if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o] {
414 unresolved "ELF weak"
415 return
416}
417
b765d4e3 418if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o] {
bd7c9df6
L
419 unresolved "ELF weak"
420 return
421}
422
b765d4e3 423if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o] {
bd7c9df6
L
424 unresolved "ELF weak"
425 return
426}
427
b765d4e3 428if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o] {
bd7c9df6
L
429 unresolved "ELF weak"
430 return
431}
432
75c8d84d 433if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/main1.c $tmpdir/main1.o] {
bd7c9df6
L
434 unresolved "ELF weak"
435 return
436}
437
b765d4e3 438if {![ld_simple_link $CC $tmpdir/libfoo1a.so "$shared $tmpdir/foo1a.o"]} {
bd7c9df6
L
439 fail "ELF weak"
440 return
441}
442
b765d4e3 443if {![ld_simple_link $CC $tmpdir/libfoo1b.so "$shared $tmpdir/foo1b.o"]} {
16a57284
L
444 fail "ELF weak"
445 return
446}
447
b765d4e3 448if {![ld_simple_link $CC $tmpdir/libbar1a.so "$shared $tmpdir/bar1a.o $tmpdir/libfoo1a.so"]} {
0c52a746
L
449 fail "ELF weak"
450 return
451}
452
bd7c9df6
L
453build_lib "ELF DSO weak func first" libfoo "foo.o bar.o" dso.dsym
454build_lib "ELF DSO weak func last" libfoo "bar.o foo.o" dso.dsym
bd7c9df6 455build_lib "ELF DSO weak func first DSO" libfoo "foo.o libbar.so" dsow.dsym
bd7c9df6
L
456build_lib "ELF DSO weak func last DSO" libfoo "libbar.so foo.o" dsow.dsym
457build_exec "ELF weak func first" foo "main.o bar.o" "" strong "" strong.sym
458build_exec "ELF weak func last" foo "bar.o main.o" "" strong "" strong.sym
75c8d84d 459build_exec "ELF weak func first DSO" foo "main.o libbar.so" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
75c8d84d 460build_exec "ELF weak func last DSO" foo "libbar.so main.o" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
bd7c9df6
L
461
462build_lib "ELF DSO weak data first" libfoo "bar1a.o foo1a.o" dsodata.dsym
463build_lib "ELF DSO weak data last" libfoo "foo1a.o bar1a.o" dsodata.dsym
0c52a746 464build_lib "ELF DSO weak data first DSO" libfoo "main1.o libfoo1a.so" dsowdata.dsym
0c52a746 465build_lib "ELF DSO weak data last DSO" libfoo "libfoo1a.so main1.o" dsowdata.dsym
0c52a746 466build_lib "ELF DSO weak data first DSO common" libfoo "main1.o libfoo1b.so" dsowdata.dsym
0c52a746 467build_lib "ELF DSO weak data last DSO common" libfoo "libfoo1b.so main1.o" dsowdata.dsym
bd7c9df6
L
468build_exec "ELF weak data first" foo "main1.o bar1a.o foo1a.o" "" strongdata "" strongdata.sym
469build_exec "ELF weak data last" foo "foo1a.o main1.o bar1a.o" "" strongdata "" strongdata.sym
470build_exec "ELF weak data first common" foo "main1.o bar1a.o foo1b.o" "" strongdata "" strongcomm.sym
471build_exec "ELF weak data last common" foo "foo1b.o main1.o bar1a.o" "" strongdata "" strongcomm.sym
75c8d84d 472build_exec "ELF weak data first DSO" foo "main1.o libbar1a.so libfoo1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
75c8d84d 473build_exec "ELF weak data last DSO" foo "libfoo1a.so main1.o libbar1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
75c8d84d 474build_exec "ELF weak data first DSO common" foo "main1.o libbar1a.so libfoo1b.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
75c8d84d 475build_exec "ELF weak data last DSO common" foo "libfoo1b.so main1.o libbar1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
a664545d
L
476
477if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_foo.c $tmpdir/size_foo.o] {
478 unresolved "ELF weak (size)"
479 return
480}
481
482if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o] {
483 unresolved "ELF weak (size)"
484 return
485}
486
487build_lib "ELF DSO small bar (size)" libsize_bar "size_bar.o" ""
488build_lib "ELF DSO foo with small bar (size)" libsize_foo "size_foo.o libsize_bar.so" ""
489
490if ![ld_compile "$CC $CFLAGS $picflag -DSIZE_BIG" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o] {
491 unresolved "ELF weak (size)"
492 return
493}
494
495build_lib "ELF DSO big bar (size)" libsize_bar "size_bar.o" ""
496
497if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/size_main.c $tmpdir/size_main.o] {
498 unresolved "ELF weak (size)"
499 return
500}
501
75c8d84d 502build_exec "ELF weak size" size_main "size_main.o libsize_foo.so libsize_bar.so" "-Wl,-rpath,.,--no-as-needed" size "" ""
5cfd5a0c
PB
503
504verbose "size2"
505run_dump_test $srcdir/$subdir/size2
This page took 0.727666 seconds and 4 git commands to generate.