1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
# Expect script for binutils tests with LTO
# Copyright (C) 2025-2026 Free Software Foundation, Inc.
#
# This file is part of the GNU Binutils.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Make sure that binutils can correctly handle LTO IR in ELF.
if { !([istarget *-*-linux*]
|| [istarget arm*-*-uclinuxfdpiceabi]
|| [istarget *-*-gnu*]) || [istarget *ecoff] } then {
return
}
# Check to see if the C and C++ compilers work
if { ![check_compiler_available] || [which $CXX_FOR_TARGET] == 0 } {
return
}
# These tests require plugin and LTO.
if { ![check_plugin_api_available]
|| ![check_lto_available] } {
return
}
set lto_fat ""
set lto_no_fat ""
if { [check_lto_fat_available] } {
set lto_fat "-ffat-lto-objects"
set lto_no_fat "-fno-fat-lto-objects"
set no_lto "-fno-lto"
}
# List contains test-items:
# 0:program name
# 1:program options
# 2:input file
# 3:output file
# 4:action list (optional)
#
proc run_lto_binutils_test { lto_tests } {
global srcdir
global subdir
global nm
global objcopy
global objdump
global READELF
global strip
global plug_opt
foreach testitem $lto_tests {
set prog_name [lindex $testitem 0]
set prog_options [lindex $testitem 1]
set input tmpdir/[lindex $testitem 2]
set output tmpdir/[lindex $testitem 3]
set actions [lindex $testitem 4]
set objfiles {}
set is_unresolved 0
set failed 0
# eval set prog \$$prog_name
switch -- $prog_name {
objcopy
{
set prog $objcopy
set prog_output "$output"
}
strip
{
set prog $strip
set prog_output "-o $output"
}
default
{
perror "Unrecognized action $action"
set is_unresolved 1
break
}
}
# Don't leave previous output around
if { $output ne "tmpdir/" } {
remote_file host delete $output
}
append prog_options " $plug_opt"
set cmd_options "$prog_options $prog_output $input"
set test_name "$prog_name $cmd_options"
set cmd "$prog $cmd_options"
send_log "$cmd\n"
set got [remote_exec host "$cmd"]
if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
send_log "$got\n"
fail "$test_name"
continue
}
if { $failed == 0 } {
foreach actionlist $actions {
set action [lindex $actionlist 0]
set progopts [lindex $actionlist 1]
# There are actions where we run regexp_diff on the
# output, and there are other actions (presumably).
# Handling of the former look the same.
set dump_prog ""
switch -- $action {
objdump
{ set dump_prog $objdump }
nm
{ set dump_prog "$nm $plug_opt" }
readelf
{ set dump_prog $READELF }
default
{
perror "Unrecognized action $action"
set is_unresolved 1
break
}
}
if { $dump_prog != "" } {
set dumpfile [lindex $actionlist 2]
set binary $dump_prog
# Ensure consistent sorting of symbols
if {[info exists env(LC_ALL)]} {
set old_lc_all $env(LC_ALL)
}
set env(LC_ALL) "C"
set cmd "$binary $progopts $output > tmpdir/dump.out"
send_log "$cmd\n"
catch "exec $cmd" comp_output
if {[info exists old_lc_all]} {
set env(LC_ALL) $old_lc_all
} else {
unset env(LC_ALL)
}
set comp_output [prune_warnings $comp_output]
if ![string match "" $comp_output] then {
send_log "$comp_output\n"
set failed 1
break
}
if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/$dumpfile"] } then {
verbose -log "output is [file_contents "tmpdir/dump.out"]" 2
set failed 1
break
}
}
}
}
if { $failed } {
fail $test_name
} elseif { $is_unresolved } {
unresolved $test_name
} else {
pass $test_name
}
}
}
run_cc_link_tests [list \
[list \
"Build strip-1a.o" \
"" \
"-O2 -flto $lto_no_fat" \
{ strip-1a.c } \
] \
[list \
"Build libstrip-1a.a" \
"$plug_opt" \
"-O2 -flto $lto_no_fat" \
{ strip-1a.c } \
{} \
"libstrip-1a.a" \
] \
[list \
"Build strip-1a-fat.o" \
"" \
"-O2 -flto $lto_fat" \
{ strip-1a-fat.c } \
] \
[list \
"Build libstrip-1a-fat.a" \
"$plug_opt" \
"-O2 -flto $lto_fat" \
{ strip-1a-fat.c } \
{} \
"libstrip-1a-fat.a" \
] \
]
run_lto_binutils_test [list \
[list \
"strip" \
"--strip-unneeded" \
"libstrip-1a.a" \
"libstrip-1a-s.a" \
] \
[list \
"strip" \
"--strip-unneeded" \
"strip-1a.o" \
"strip-1a-s.o" \
] \
[list \
"strip" \
"--strip-unneeded -R .gnu.*lto_* -N __gnu_lto_v1" \
"libstrip-1a-fat.a" \
"libstrip-1a-fat-s.a" \
{{readelf -SW strip-1a-fat.rd}} \
] \
[list \
"strip" \
"--strip-unneeded -R .gnu.*lto_* -N __gnu_lto_v1" \
"strip-1a-fat.o" \
"strip-1a-fat-s.o" \
{{readelf -SW strip-1a-fat.rd}} \
] \
[list \
"strip" \
"--strip-unneeded -R .gnu.debuglto_*" \
"libstrip-1a-fat.a" \
"libstrip-1b-fat-s.a" \
{{readelf -SW strip-1b-fat.rd}} \
] \
[list \
"strip" \
"--strip-unneeded -R .gnu.debuglto_*" \
"strip-1a-fat.o" \
"strip-1b-fat-s.o" \
{{readelf -SW strip-1b-fat.rd}} \
] \
]
run_cc_link_tests [list \
[list \
"Build strip-1a (strip-1a.o)" \
"" \
"-O2 -flto $lto_no_fat" \
{ strip-1b.c } \
{} \
"libstrip-1a" \
"C" \
"tmpdir/strip-1a.o" \
] \
[list \
"Build strip-1b (strip-1a-s.o)" \
"" \
"-O2 -flto $lto_no_fat" \
{ strip-1b.c } \
{} \
"libstrip-1b" \
"C" \
"tmpdir/strip-1a-s.o" \
] \
[list \
"Build strip-1c (libstrip-1a.a)" \
"" \
"-O2 -flto $lto_no_fat" \
{ strip-1b.c } \
{} \
"libstrip-1c" \
"C" \
"tmpdir/libstrip-1a.a" \
] \
[list \
"Build strip-1d (libstrip-1a-s.a)" \
"" \
"-O2 -flto $lto_no_fat" \
{ strip-1b.c } \
{} \
"libstrip-1d" \
"C" \
"tmpdir/libstrip-1a-s.a" \
] \
[list \
"Build strip-1e (strip-1a-fat-s.o)" \
"" \
"-O2 -flto $lto_fat" \
{ strip-1b-fat.c } \
{} \
"libstrip-1e" \
"C" \
"tmpdir/strip-1a-fat-s.o" \
] \
[list \
"Build strip-1f (libstrip-1a-fat-s.a)" \
"" \
"-O2 -flto $lto_fat" \
{ strip-1b-fat.c } \
{} \
"libstrip-1f" \
"C" \
"tmpdir/libstrip-1a-fat-s.a" \
] \
[list \
"Build strip-1g (strip-1b-fat-s.o)" \
"" \
"-O2 -flto $lto_fat" \
{ strip-1b-fat.c } \
{} \
"libstrip-1g" \
"C" \
"tmpdir/strip-1b-fat-s.o" \
] \
[list \
"Build strip-1h (libstrip-1b-fat-s.a)" \
"" \
"-O2 -flto $lto_fat" \
{ strip-1b-fat.c } \
{} \
"libstrip-1h" \
"C" \
"tmpdir/libstrip-1b-fat-s.a" \
] \
]
proc run_pr33246_test { llvm fat } {
global srcdir
global subdir
global plug_opt
global llvm_plug_opt
global ar
global CLANG_FOR_TARGET
global CC_FOR_TARGET
global NM
global READELF
global strip
set strip_flags "--strip-debug --enable-deterministic-archives"
set test pr33246
set testname "${test}${llvm}${fat} with $strip_flags"
if { "$llvm" == "-llvm" } {
# Skip native x32 and i?86 targets since system LLVMgold.so may
# not be compatible with native x32 and i?86 targets binutils.
if { [istarget "x86_64-*-linux*-gnux32"]
|| [istarget "i?86-*-*"]
|| ![info exists CLANG_FOR_TARGET]
|| [string match "" $llvm_plug_opt] } then {
untested $testname
return
}
set CC $CLANG_FOR_TARGET
set binutils_plug_opt "$llvm_plug_opt"
} else {
if { ![info exists CC_FOR_TARGET]
|| [string match "" $plug_opt] } then {
untested $testname
return
}
set CC $CC_FOR_TARGET
set binutils_plug_opt "$plug_opt"
}
append strip_flags " $binutils_plug_opt"
set src $srcdir/$subdir/${test}.c
set obj tmpdir/${test}${llvm}${fat}.o
set archive tmpdir/${test}${llvm}${fat}.a
set CFLAGS "-c -g -O2 -flto"
if { "$fat" == "-fat" } {
append CFLAGS " -ffat-lto-objects"
} else {
append CFLAGS " -fno-fat-lto-objects"
}
set cmd "$CC $CFLAGS -o $obj $src"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![string match "" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname ($obj)"
return
}
set cmd "$strip $strip_flags $obj -o ${obj}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![string match "" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $obj)"
return
}
set cmd "$NM $binutils_plug_opt ${obj}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![regexp "0+ T foo" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $obj)"
return
}
if { "$fat" == "-fat" } {
set cmd "$READELF -SW ${obj}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if [regexp " \.debug_" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $obj)"
return
}
} elseif { "$llvm" == "-llvm" } {
set cmd "cmp $obj ${obj}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![string match "" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $obj)"
return
}
}
pass "$testname (strip $obj)"
set cmd "$ar $binutils_plug_opt -D -s -r -c $archive $obj"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![string match "" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname ($archive)"
return
}
set cmd "$strip $strip_flags $archive -o ${archive}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![string match "" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $archive)"
return
}
set cmd "$NM $binutils_plug_opt ${archive}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![regexp "0+ T foo" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $archive)"
return
}
if { "$fat" == "-fat" } {
set cmd "$READELF -SW ${archive}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if [regexp " \.debug_" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $archive)"
return
}
} elseif { "$llvm" == "-llvm" } {
set cmd "cmp $archive ${archive}.strip"
send_log "$cmd\n"
verbose "$cmd" 1
catch "exec $cmd" got
if ![string match "" $got] then {
send_log "$got\n"
verbose "$got" 1
fail "$testname (strip $archive)"
return
}
}
pass "$testname (strip $archive)"
}
run_pr33246_test "" ""
run_pr33246_test "" "-fat"
run_pr33246_test "-llvm" ""
run_pr33246_test "-llvm" "-fat"
|