[PATCH v2 3/3] binutils/testsuite: test compress-debug-info round trip

Tobias Deiminger tobias.deiminger@linutronix.de
Thu Nov 26 08:51:55 GMT 2026


This test checks if debug info is usable after compression and again
after decompression for many BFD targets. It basically runs

 target_cc testprog.c testprog
 for every testable BFD target
   objcopy -I <compiled_target> -O <test_target> testprog  # convert
   objcopy --compress-debug-sections           # compress
   objdump --dwarf=info                        # debug info expected
   objcopy --decompress-debug-sections         # decompress
   objdump --dwarf=info                        # debug info expected

The basic test idea is to generate multiple test targets from a single
compiled program to maximize test coverage with minimum toolchain
dependencies. This can only work for targets that have the same
architecture and endianness as the compiled test program and can hold
debug sections. The latter requirement excludes raw formats like srec
since they can't hold named sections, a.out since it has a fixed set of
sections where DWARF is not included, and COFF without long section
names, since it truncates .debug_*.

The actual test looks for a fixed set of expected debug info entries in
the binary's DWARF sections.

Failures caused by target conversion are reported as unsupported rather
than failed, because
- conversion is only preparation, not the test expectation, and
- find_testable_targets has no means to check for machine compatibility.
  The arch compatibility heuristic is intentionally too wide, to not
  exclude working targets early.

Coverage can be increased by repeating the test run with different
--target= toolchain settings.

Signed-off-by: Tobias Deiminger <tobias.deiminger@linutronix.de>
---

Notes:
    I tested the following configurations locally, leading to following
    coverage:
    
    configure --enable-targets=all on x86_64-pc-linux-gnu, 14 targets
    covered:
    coff-go32 coff-go32-exe elf32-little elf32-x86-64 elf64-little
    elf64-x86-64 elf64-x86-64-freebsd elf64-x86-64-sol2 pe-bigobj-i386
    pe-bigobj-x86-64 pe-i386 pe-x86-64 pei-i386 pei-x86-64
    
    configure --target=x86_64-w64-mingw32, 10 targets covered:
    elf32-i386 elf32-little elf64-little elf64-x86-64 pe-bigobj-i386
    pe-bigobj-x86-64 pe-i386 pe-x86-64 pei-i386 pei-x86-64
    
    configure --target=i686-w64-mingw32, 5 targets covered:
    elf32-i386 elf32-little pe-bigobj-i386 pe-i386 pei-i386
    
    configure --target=aarch64-linux-gnu, 6 targets covered:
    elf32-little elf32-littleaarch64 elf64-little elf64-littleaarch64
    pe-aarch64-little pei-aarch64-little
    
    configure --target=riscv64-linux-gnu, 5 targets covered:
    elf32-little elf32-littleriscv elf64-little elf64-littleriscv
    pei-riscv64-little
    
    configure --target=arm-linux-gnueabihf, 3 targets covered:
    elf32-little elf32-littlearm elf32-littlearm-fdpic
    
    configure --target=powerpc64le-linux-gnu, 4 targets covered:
    elf32-little elf32-powerpcle elf64-little elf64-powerpcle
    
    Note that the new test reveals another unrelated bug where
    
     objcopy -I elf64-x86-64 -O elf32-i386 testprog testprog-elf32-i386
    
    produces invalid output that can't be processed further by objdump
    ("file format not recognized"). The issue isn't fixed here, since it
    doesn't block the series. The test skips over it as unsupported.

 binutils/testsuite/binutils-all/compress.exp | 156 ++++++++++++++++++-
 1 file changed, 155 insertions(+), 1 deletion(-)

diff --git a/binutils/testsuite/binutils-all/compress.exp b/binutils/testsuite/binutils-all/compress.exp
index ae2bf8010f8..f5888b040f8 100644
--- a/binutils/testsuite/binutils-all/compress.exp
+++ b/binutils/testsuite/binutils-all/compress.exp
@@ -14,9 +14,163 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 
+if { [is_remote host] } then {
+    return
+}
+
+proc find_testable_targets { info input_target known_unsupported } {
+    set info_lines [split $info \n]
+
+    set target_name ""
+    foreach line $info_lines {
+	if { [regexp {^(\S+)$} $line -> target_name] } {
+	    continue
+	}
+	if { [regexp {^ \(header [^,]*, data ([^)]*)\)$} $line -> endianness] } {
+	    set target_endianness($target_name) $endianness
+	    continue
+	}
+	if { [regexp {^  (\S+)$} $line -> architecture] } {
+	    lappend target_architectures($target_name) $architecture
+	    continue
+	}
+	if { [string equal "" $line] } {
+	    break
+	}
+    }
+
+    set filtered_targets {}
+    if { ![info exists target_endianness($input_target)]
+	 || ![info exists target_architectures($input_target)] } {
+	return $filtered_targets
+    }
+    foreach target [array names target_architectures] {
+	set exclude_unsupported 0
+	foreach pattern $known_unsupported {
+	    if { [string match $pattern $target] } {
+		set exclude_unsupported 1
+		break
+	    }
+	}
+	set arch_compatible 0
+	foreach arch $target_architectures($input_target) {
+	    if { [lsearch -exact $target_architectures($target) $arch] >= 0 } {
+		set arch_compatible 1
+		break
+	    }
+	}
+	if { $target_endianness($target) eq $target_endianness($input_target)
+	    && $arch_compatible
+	    && !$exclude_unsupported } {
+	    lappend filtered_targets $target
+	}
+    }
+    return [lsort $filtered_targets]
+}
+
+proc has_testprog_debug_info { target testprog_bin expected } {
+    global OBJDUMP
+
+    set dwarf_info_lines [split [binutils_run $OBJDUMP "-b $target --dwarf=info $testprog_bin"] \n]
+    set found_nr 0
+    set dw_lines_nr [llength $dwarf_info_lines]
+    for {set i 0} {$i < $dw_lines_nr} {incr i} {
+	set line [lindex $dwarf_info_lines $i]
+	set is_entry_start [string equal -length 2 " <" $line]
+	if { !$is_entry_start && [regexp {DW_AT_(\S+)\s*:.*\s(\S+)$} $line -> attr value] } {
+	    set dw_entry($attr) $value
+	}
+	if { $is_entry_start || $i == $dw_lines_nr - 1 } {
+	    if { [info exists dw_entry(name)] && [info exists dw_entry(decl_line)]
+		&& [lsearch -exact $expected [list $dw_entry(name) $dw_entry(decl_line)]] >= 0 } {
+		incr found_nr
+	    }
+	    array unset dw_entry
+	}
+    }
+    return [expr {$found_nr == [llength $expected]}]
+}
+
+proc test_compress_decompress_roundtrip_all_targets {} {
+    global binutils_run_status
+    global srcdir
+    global subdir
+    global OBJCOPY
+    global OBJDUMP
+
+    set test "objcopy compress debug sections round trip"
+    set known_unsupported_targets {
+	msdos binary srec symbolsrec ihex verilog wasm
+	a.out-* coff-i386 coff-i386-lynx
+    }
+    set test_compiled "tmpdir/testprog[exeext]"
+
+    if { [target_compile $srcdir/$subdir/testprog.c $test_compiled executable debug] != "" } {
+	unsupported "$test (can't compile testprog)"
+	return
+    }
+
+    set objdump_out [binutils_run $OBJDUMP "-f $test_compiled"]
+    if { ![regexp {file format (\S+)} $objdump_out -> input_target] } {
+	unsupported "$test (can't determine BFD input target)"
+	return
+    }
+
+    set info [binutils_run $OBJCOPY "--info"]
+    set testable_targets [find_testable_targets $info $input_target $known_unsupported_targets]
+
+    set tested 0
+    set expected { {fn 12} {main 18} }
+    foreach target $testable_targets {
+	set test_in "tmpdir/testprog-$target"
+	set test_z   "${test_in}.z"
+	set test_unz "${test_in}.unz"
+
+	binutils_run $OBJCOPY "-I $input_target -O $target $test_compiled $test_in"
+	if { $binutils_run_status != 0 } {
+	    unsupported "$test ($target, no conversion from $input_target)"
+	    continue
+	}
+	if { ![has_testprog_debug_info $target $test_in $expected] } {
+	    unsupported "$test ($target, debug info missing after conversion from $input_target)"
+	    continue
+	}
+
+	incr tested
+	set testname "$test ($target)"
+
+	binutils_run $OBJCOPY "-I $target --compress-debug-sections $test_in $test_z"
+	if { $binutils_run_status != 0 } {
+	    fail $testname
+	    continue
+	}
+	if { ![has_testprog_debug_info $target $test_z $expected] } {
+	    fail $testname
+	    continue
+	}
+
+	binutils_run $OBJCOPY "-I $target --decompress-debug-sections $test_z $test_unz"
+	if { $binutils_run_status != 0 } {
+	    fail $testname
+	    continue
+	}
+	if { ![has_testprog_debug_info $target $test_unz $expected] } {
+	    fail $testname
+	    continue
+	}
+	pass $testname
+    }
+
+    if { $tested == 0 } {
+	unsupported "$test (no compatible targets found)"
+    }
+}
+
+test_compress_decompress_roundtrip_all_targets
+
 # Test compressed .debug section.
 
-if { [is_remote host] || ![is_elf_format] } then {
+if { ![is_elf_format] } then {
     return
 }
 
-- 
2.55.0



More information about the Binutils mailing list