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