1# Expect script for ld-weak tests
2#   Copyright (C) 2001-2017 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# 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.
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*]
51     && ![istarget sparc*-*-linux*]
52     && ![istarget x86_64-*-linux*]
53     && ![istarget *-*-nacl*] } {
54    return
55}
56
57if { [istarget *-*-linux*aout*]
58     || [istarget *-*-linux*oldld*] } {
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
70set shared "--shared -Wl,--no-as-needed"
71
72
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    set cmd "$objdump $SOBJDUMP_FLAGS $object | sed -n {s/^\\(\[0-9a-f\]* *\\)\\(\[gw\]\\)\\( *\\)\\(\[FO\]\\)/\\1\\2\\4\\3/;/foo$/p} > $tmpdir/objdump.out"
86    verbose -log $cmd
87    catch "exec $cmd" 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	}
103
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 {\.text.* \.[^ ]*$} $line] then {
127		# Discard defined powerpc64 dot-symbols
128		continue
129	    } else {
130		lappend list_b $line
131	    }
132	}
133	close $file_b
134
135	for { set i 0 } { $i < [llength $list_a] } { incr i } {
136	    set line_a [lindex $list_a $i]
137	    set line_b [lindex $list_b $i]
138
139
140	    verbose "\t$expectfile: $i: $line_a" 3
141	    verbose "\t/tmp/objdump.out: $i: $line_b" 3
142	    if [regexp $line_a $line_b] then {
143		continue
144	    } else {
145		verbose -log "\t$expectfile: $i: $line_a"
146		verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
147
148		return 0
149	    }
150	}
151
152	if { [llength $list_a] != [llength $list_b] } {
153	    verbose -log "Line count"
154	    return 0
155	}
156
157	if $differences<1 then {
158	    return 1
159	}
160
161	return 0
162    } else {
163	verbose -log "$exec_output"
164	return 0
165    }
166
167}
168
169#
170# objdump_dymsymstuff
171#	Dump dynamic symbol stuff and make sure that it is sane.
172#
173proc objdump_dynsymstuff { objdump object expectfile } {
174    global DOBJDUMP_FLAGS
175    global version_output
176    global diff
177    global tmpdir
178
179    if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
180
181    set cmd "$objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out"
182    verbose -log $cmd
183    catch "exec $cmd" exec_output
184    set exec_output [prune_warnings $exec_output]
185    if [string match "" $exec_output] then {
186
187# Now do a line-by-line comparison to effectively diff the darned things
188# The stuff coming from the expectfile is actually a regex, so we can
189# skip over the actual addresses and so forth.  This is currently very
190# simpleminded - it expects a one-to-one correspondence in terms of line
191# numbers.
192
193	if [file exists $expectfile] then {
194	    set file_a [open $expectfile r]
195	} else {
196	    warning "$expectfile doesn't exist"
197	    return 0
198	}
199
200	if [file exists $tmpdir/objdump.out] then {
201	    set file_b [open $tmpdir/objdump.out r]
202	} else {
203	    fail "$tmpdir/objdump.out doesn't exist"
204	    return 0
205	}
206
207	verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
208
209	set eof -1
210	set differences 0
211
212	while { [gets $file_a line] != $eof } {
213	    if [regexp "^#.*$" $line] then {
214		continue
215	    } else {
216		lappend list_a $line
217	    }
218	}
219	close $file_a
220
221	while { [gets $file_b line] != $eof } {
222	    if [regexp {\.text.* \.[^ ]*$} $line] then {
223		# Discard defined powerpc64 dot-symbols
224		continue
225	    } else {
226		lappend list_b $line
227	    }
228	}
229	close $file_b
230
231	for { set i 0 } { $i < [llength $list_b] } { incr i } {
232	    set line_b [lindex $list_b $i]
233
234# The tests are rigged so that we should never export a symbol with the
235# word 'hide' in it.  Thus we just search for it, and bail if we find it.
236	    if [regexp "hide" $line_b] then {
237		verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
238
239		return 0
240	    }
241
242	    verbose "\t$expectfile: $i: $line_b" 3
243
244	    # We can't assume that the sort is consistent across
245	    # systems, so we must check each regexp.  When we find a
246	    # regexp, we null it out, so we don't match it twice.
247	    for { set j 0 } { $j < [llength $list_a] } { incr j } {
248		set line_a [lindex $list_a $j]
249
250		if [regexp $line_a $line_b] then {
251		    lreplace $list_a $j $j "CAN NOT MATCH"
252		    break
253		}
254	    }
255
256	    if { $j >= [llength $list_a] } {
257		verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
258
259		return 0
260	    }
261	}
262
263	if { [llength $list_a] != [llength $list_b] } {
264	    verbose -log "Line count"
265	    return 0
266	}
267
268	if $differences<1 then {
269	    return 1
270	}
271
272	return 0
273    } else {
274	verbose -log "$exec_output"
275	return 0
276    }
277
278}
279
280proc build_lib {test libname objs dynsymexp} {
281    global CC
282    global objdump
283    global tmpdir
284    global shared
285    global srcdir
286    global subdir
287
288    set files ""
289    foreach obj $objs {
290      set files "$files $tmpdir/$obj"
291    }
292
293    if {![ld_simple_link $CC $tmpdir/$libname.so "$shared $files"]} {
294	fail $test
295	return
296    }
297
298    if {![string match "" $dynsymexp]
299	&& ![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$dynsymexp]} {
300	fail $test
301	return
302    }
303    pass $test
304}
305
306proc build_exec { test execname objs flags dat dynsymexp symexp} {
307    global CC
308    global objdump
309    global tmpdir
310    global srcdir
311    global subdir
312    global exec_output
313
314    set files ""
315    foreach obj $objs {
316      set files "$files $tmpdir/$obj"
317    }
318
319    if {![ld_simple_link $CC $tmpdir/$execname "$flags $files"]} {
320	fail "$test"
321	return
322    }
323
324    if {![string match "" $dynsymexp]} then {
325        if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$dynsymexp]} {
326	    fail $test
327	    return
328	}
329    }
330
331    if {![string match "" $symexp]} then {
332        if {![objdump_symstuff $objdump $tmpdir/$execname $srcdir/$subdir/$symexp]} {
333	    fail $test
334	    return
335	}
336    }
337
338    # Run the resulting program
339    send_log "$tmpdir/$execname >$tmpdir/$execname.out\n"
340    verbose "$tmpdir/$execname >$tmpdir/$execname.out"
341    catch "exec $tmpdir/$execname >$tmpdir/$execname.out" exec_output
342    if ![string match "" $exec_output] then {
343	send_log "$exec_output\n"
344	verbose "$exec_output"
345	fail $test
346	return
347    }
348
349    send_log "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat\n"
350    verbose "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat"
351    catch "exec diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat" exec_output
352    set exec_output [prune_warnings $exec_output]
353
354    if {![string match "" $exec_output]} then {
355	send_log "$exec_output\n"
356	verbose "$exec_output"
357	fail $test
358        return
359    }
360
361    pass $test
362}
363
364# Old version of GCC for MIPS default to enabling -fpic
365# and get confused if it is used on the command line.
366if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
367    set picflag ""
368} else {
369    # Unfortunately, the gcc argument is -fpic and the cc argument is
370    # -KPIC.  We have to try both.
371    set picflag "-fpic"
372    send_log "$CC $picflag\n"
373    verbose "$CC $picflag"
374    catch "exec $CC $picflag" exec_output
375    send_log "$exec_output\n"
376    verbose "--" "$exec_output"
377    if { [string match "*illegal option*" $exec_output]
378	 || [string match "*option ignored*" $exec_output]
379	 || [string match "*unrecognized option*" $exec_output]
380	 || [string match "*passed to ld*" $exec_output] } {
381	if [istarget *-*-sunos4*] {
382	    set picflag "-pic"
383	} else {
384	    set picflag "-KPIC"
385	}
386    }
387}
388verbose "Using $picflag to compile PIC code"
389
390if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o] {
391    unresolved "ELF weak"
392    return
393}
394
395if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o] {
396    unresolved "ELF weak"
397    return
398}
399
400if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main.c $tmpdir/main.o] {
401    unresolved "ELF weak"
402    return
403}
404
405if {![ld_simple_link $CC $tmpdir/libbar.so "$shared $tmpdir/bar.o"]} {
406    fail "ELF weak"
407    return
408}
409
410if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o] {
411    unresolved "ELF weak"
412    return
413}
414
415if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o] {
416    unresolved "ELF weak"
417    return
418}
419
420if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o] {
421    unresolved "ELF weak"
422    return
423}
424
425if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o] {
426    unresolved "ELF weak"
427    return
428}
429
430if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o] {
431    unresolved "ELF weak"
432    return
433}
434
435if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/main1.c $tmpdir/main1.o] {
436    unresolved "ELF weak"
437    return
438}
439
440if {![ld_simple_link $CC $tmpdir/libfoo1a.so "$shared $tmpdir/foo1a.o"]} {
441    fail "ELF weak"
442    return
443}
444
445if {![ld_simple_link $CC $tmpdir/libfoo1b.so "$shared $tmpdir/foo1b.o"]} {
446    fail "ELF weak"
447    return
448}
449
450if {![ld_simple_link $CC $tmpdir/libbar1a.so "$shared $tmpdir/bar1a.o $tmpdir/libfoo1a.so"]} {
451    fail "ELF weak"
452    return
453}
454
455build_lib "ELF DSO weak func first" libfoo "foo.o bar.o" dso.dsym
456build_lib "ELF DSO weak func last" libfoo "bar.o foo.o" dso.dsym
457build_lib "ELF DSO weak func first DSO" libfoo "foo.o libbar.so" dsow.dsym
458build_lib "ELF DSO weak func last DSO" libfoo "libbar.so foo.o" dsow.dsym
459build_exec "ELF weak func first" foo "main.o bar.o" "" strong "" strong.sym
460build_exec "ELF weak func last" foo "bar.o main.o" "" strong "" strong.sym
461build_exec "ELF weak func first DSO" foo "main.o libbar.so" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
462build_exec "ELF weak func last DSO" foo "libbar.so main.o" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
463
464build_lib "ELF DSO weak data first" libfoo "bar1a.o foo1a.o" dsodata.dsym
465build_lib "ELF DSO weak data last" libfoo "foo1a.o bar1a.o" dsodata.dsym
466build_lib "ELF DSO weak data first DSO" libfoo "main1.o libfoo1a.so" dsowdata.dsym
467build_lib "ELF DSO weak data last DSO" libfoo "libfoo1a.so main1.o" dsowdata.dsym
468build_lib "ELF DSO weak data first DSO common" libfoo "main1.o libfoo1b.so" dsowdata.dsym
469build_lib "ELF DSO weak data last DSO common" libfoo "libfoo1b.so main1.o" dsowdata.dsym
470build_exec "ELF weak data first" foo "main1.o bar1a.o foo1a.o" "" strongdata "" strongdata.sym
471build_exec "ELF weak data last" foo "foo1a.o main1.o bar1a.o" "" strongdata "" strongdata.sym
472build_exec "ELF weak data first common" foo "main1.o bar1a.o foo1b.o" "" strongdata "" strongcomm.sym
473build_exec "ELF weak data last common" foo "foo1b.o main1.o bar1a.o" "" strongdata "" strongcomm.sym
474build_exec "ELF weak data first DSO" foo "main1.o libbar1a.so libfoo1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
475build_exec "ELF weak data last DSO" foo "libfoo1a.so main1.o libbar1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
476build_exec "ELF weak data first DSO common" foo "main1.o libbar1a.so libfoo1b.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
477build_exec "ELF weak data last DSO common" foo "libfoo1b.so main1.o libbar1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
478
479if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_foo.c $tmpdir/size_foo.o] {
480    unresolved "ELF weak (size)"
481    return
482}
483
484if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o] {
485    unresolved "ELF weak (size)"
486    return
487}
488
489build_lib "ELF DSO small bar (size)" libsize_bar "size_bar.o" ""
490build_lib "ELF DSO foo with small bar (size)" libsize_foo "size_foo.o libsize_bar.so" ""
491
492if ![ld_compile "$CC $CFLAGS $picflag -DSIZE_BIG" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o] {
493    unresolved "ELF weak (size)"
494    return
495}
496
497build_lib "ELF DSO big bar (size)" libsize_bar "size_bar.o" ""
498
499if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/size_main.c $tmpdir/size_main.o] {
500    unresolved "ELF weak (size)"
501    return
502}
503
504build_exec "ELF weak size" size_main "size_main.o libsize_foo.so libsize_bar.so" "-Wl,-rpath,.,--no-as-needed" size "" ""
505
506verbose "size2"
507run_dump_test $srcdir/$subdir/size2
508