readonly_dynrelocs
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfweak / elfweak.exp
CommitLineData
16a57284 1# Expect script for ld-weak tests
2571583a 2# Copyright (C) 2001-2017 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
d9816402
AM
25# Check to see if the C compiler works
26if { [which $CC] == 0 } {
27 return
28}
16a57284
L
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.
99753d9d
AM
33if { ![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*]
5a68afcf 53 && ![istarget sparc*-*-linux*]
75c8d84d 54 && ![istarget x86_64-*-linux*]
5a68afcf 55 && ![istarget *-*-nacl*] } {
16a57284
L
56 return
57}
58
99753d9d
AM
59if { [istarget *-*-linux*aout*]
60 || [istarget *-*-linux*oldld*] } {
16a57284
L
61 return
62}
63
64if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
65 return
66}
67
68set diff diff
69set tmpdir tmpdir
70set DOBJDUMP_FLAGS --dynamic-syms
71set SOBJDUMP_FLAGS --syms
75c8d84d 72set shared "--shared -Wl,--no-as-needed"
16a57284 73
5940a93c 74
16a57284
L
75#
76# objdump_symstuff
77# Dump non-dynamic symbol stuff and make sure that it is sane.
78#
79proc 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
4e95fbcd
AM
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
16a57284
L
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 }
5a68afcf 105
16a57284
L
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 } {
4e95fbcd
AM
128 if [regexp {\.text.* \.[^ ]*$} $line] then {
129 # Discard defined powerpc64 dot-symbols
16a57284
L
130 continue
131 } else {
132 lappend list_b $line
133 }
134 }
135 close $file_b
5a68afcf 136
16a57284
L
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]
5a68afcf 140
16a57284
L
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"
5a68afcf 149
16a57284
L
150 return 0
151 }
152 }
5a68afcf 153
16a57284
L
154 if { [llength $list_a] != [llength $list_b] } {
155 verbose -log "Line count"
156 return 0
157 }
5a68afcf 158
16a57284
L
159 if $differences<1 then {
160 return 1
161 }
5a68afcf 162
16a57284
L
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#
175proc 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
4e95fbcd
AM
183 set cmd "$objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out"
184 verbose -log $cmd
185 catch "exec $cmd" exec_output
16a57284
L
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 }
5a68afcf 201
16a57284
L
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 } {
4e95fbcd
AM
224 if [regexp {\.text.* \.[^ ]*$} $line] then {
225 # Discard defined powerpc64 dot-symbols
16a57284
L
226 continue
227 } else {
228 lappend list_b $line
229 }
230 }
231 close $file_b
5a68afcf 232
16a57284
L
233 for { set i 0 } { $i < [llength $list_b] } { incr i } {
234 set line_b [lindex $list_b $i]
5a68afcf 235
16a57284
L
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"
5a68afcf 240
16a57284
L
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"
5a68afcf 260
16a57284
L
261 return 0
262 }
263 }
5a68afcf 264
16a57284
L
265 if { [llength $list_a] != [llength $list_b] } {
266 verbose -log "Line count"
267 return 0
268 }
5a68afcf 269
16a57284
L
270 if $differences<1 then {
271 return 1
272 }
5a68afcf 273
16a57284
L
274 return 0
275 } else {
276 verbose -log "$exec_output"
277 return 0
278 }
279
280}
281
bd7c9df6 282proc build_lib {test libname objs dynsymexp} {
b765d4e3 283 global CC
16a57284
L
284 global objdump
285 global tmpdir
286 global shared
287 global srcdir
288 global subdir
289
bd7c9df6
L
290 set files ""
291 foreach obj $objs {
292 set files "$files $tmpdir/$obj"
293 }
294
d9816402 295 if {![ld_link $CC $tmpdir/$libname.so "$shared $files"]} {
16a57284
L
296 fail $test
297 return
298 }
299
99753d9d 300 if {![string match "" $dynsymexp]
a664545d 301 && ![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$dynsymexp]} {
16a57284
L
302 fail $test
303 return
304 }
305 pass $test
306}
307
bd7c9df6 308proc build_exec { test execname objs flags dat dynsymexp symexp} {
b765d4e3 309 global CC
16a57284
L
310 global objdump
311 global tmpdir
16a57284
L
312 global srcdir
313 global subdir
314 global exec_output
315
bd7c9df6
L
316 set files ""
317 foreach obj $objs {
318 set files "$files $tmpdir/$obj"
319 }
320
d9816402 321 if {![ld_link $CC $tmpdir/$execname "$flags $files"]} {
16a57284
L
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
d9816402
AM
340 if ![isnative] {
341 unsupported $test
342 return
343 }
16a57284
L
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
fb35d3d8
DD
370# Old version of GCC for MIPS default to enabling -fpic
371# and get confused if it is used on the command line.
372if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
16a57284
L
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"
99753d9d
AM
383 if { [string match "*illegal option*" $exec_output]
384 || [string match "*option ignored*" $exec_output]
385 || [string match "*unrecognized option*" $exec_output]
16a57284
L
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}
394verbose "Using $picflag to compile PIC code"
395
396if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o] {
397 unresolved "ELF weak"
398 return
399}
400
401if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o] {
402 unresolved "ELF weak"
403 return
404}
405
406if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main.c $tmpdir/main.o] {
407 unresolved "ELF weak"
408 return
409}
410
d9816402 411if {![ld_link $CC $tmpdir/libbar.so "$shared $tmpdir/bar.o"]} {
bd7c9df6
L
412 fail "ELF weak"
413 return
414}
415
416if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o] {
417 unresolved "ELF weak"
418 return
419}
420
421if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o] {
422 unresolved "ELF weak"
423 return
424}
425
b765d4e3 426if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o] {
bd7c9df6
L
427 unresolved "ELF weak"
428 return
429}
430
b765d4e3 431if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o] {
bd7c9df6
L
432 unresolved "ELF weak"
433 return
434}
435
b765d4e3 436if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o] {
bd7c9df6
L
437 unresolved "ELF weak"
438 return
439}
440
75c8d84d 441if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/main1.c $tmpdir/main1.o] {
bd7c9df6
L
442 unresolved "ELF weak"
443 return
444}
445
d9816402 446if {![ld_link $CC $tmpdir/libfoo1a.so "$shared $tmpdir/foo1a.o"]} {
bd7c9df6
L
447 fail "ELF weak"
448 return
449}
450
d9816402 451if {![ld_link $CC $tmpdir/libfoo1b.so "$shared $tmpdir/foo1b.o"]} {
16a57284
L
452 fail "ELF weak"
453 return
454}
455
d9816402 456if {![ld_link $CC $tmpdir/libbar1a.so "$shared $tmpdir/bar1a.o $tmpdir/libfoo1a.so"]} {
0c52a746
L
457 fail "ELF weak"
458 return
459}
460
bd7c9df6
L
461build_lib "ELF DSO weak func first" libfoo "foo.o bar.o" dso.dsym
462build_lib "ELF DSO weak func last" libfoo "bar.o foo.o" dso.dsym
bd7c9df6 463build_lib "ELF DSO weak func first DSO" libfoo "foo.o libbar.so" dsow.dsym
bd7c9df6
L
464build_lib "ELF DSO weak func last DSO" libfoo "libbar.so foo.o" dsow.dsym
465build_exec "ELF weak func first" foo "main.o bar.o" "" strong "" strong.sym
466build_exec "ELF weak func last" foo "bar.o main.o" "" strong "" strong.sym
75c8d84d 467build_exec "ELF weak func first DSO" foo "main.o libbar.so" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
75c8d84d 468build_exec "ELF weak func last DSO" foo "libbar.so main.o" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
bd7c9df6
L
469
470build_lib "ELF DSO weak data first" libfoo "bar1a.o foo1a.o" dsodata.dsym
471build_lib "ELF DSO weak data last" libfoo "foo1a.o bar1a.o" dsodata.dsym
0c52a746 472build_lib "ELF DSO weak data first DSO" libfoo "main1.o libfoo1a.so" dsowdata.dsym
0c52a746 473build_lib "ELF DSO weak data last DSO" libfoo "libfoo1a.so main1.o" dsowdata.dsym
0c52a746 474build_lib "ELF DSO weak data first DSO common" libfoo "main1.o libfoo1b.so" dsowdata.dsym
0c52a746 475build_lib "ELF DSO weak data last DSO common" libfoo "libfoo1b.so main1.o" dsowdata.dsym
bd7c9df6
L
476build_exec "ELF weak data first" foo "main1.o bar1a.o foo1a.o" "" strongdata "" strongdata.sym
477build_exec "ELF weak data last" foo "foo1a.o main1.o bar1a.o" "" strongdata "" strongdata.sym
478build_exec "ELF weak data first common" foo "main1.o bar1a.o foo1b.o" "" strongdata "" strongcomm.sym
479build_exec "ELF weak data last common" foo "foo1b.o main1.o bar1a.o" "" strongdata "" strongcomm.sym
75c8d84d 480build_exec "ELF weak data first DSO" foo "main1.o libbar1a.so libfoo1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
75c8d84d 481build_exec "ELF weak data last DSO" foo "libfoo1a.so main1.o libbar1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
8cfc7cf8
AM
482build_exec "ELF weak data first DSO common" foo "main1.o libbar1a.so libfoo1b.so" "-Wl,--no-as-needed,-rpath,.,-rpath-link,." weakdata weakdata.dsym ""
483build_exec "ELF weak data last DSO common" foo "libfoo1b.so main1.o libbar1a.so" "-Wl,--no-as-needed,-rpath,.,-rpath-link,." weakdata weakdata.dsym ""
a664545d
L
484
485if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_foo.c $tmpdir/size_foo.o] {
486 unresolved "ELF weak (size)"
487 return
488}
489
490if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o] {
491 unresolved "ELF weak (size)"
492 return
493}
494
495build_lib "ELF DSO small bar (size)" libsize_bar "size_bar.o" ""
496build_lib "ELF DSO foo with small bar (size)" libsize_foo "size_foo.o libsize_bar.so" ""
497
498if ![ld_compile "$CC $CFLAGS $picflag -DSIZE_BIG" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o] {
499 unresolved "ELF weak (size)"
500 return
501}
502
503build_lib "ELF DSO big bar (size)" libsize_bar "size_bar.o" ""
504
505if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/size_main.c $tmpdir/size_main.o] {
506 unresolved "ELF weak (size)"
507 return
508}
509
75c8d84d 510build_exec "ELF weak size" size_main "size_main.o libsize_foo.so libsize_bar.so" "-Wl,-rpath,.,--no-as-needed" size "" ""
5cfd5a0c
PB
511
512verbose "size2"
513run_dump_test $srcdir/$subdir/size2
This page took 0.835283 seconds and 4 git commands to generate.