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