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