From: Simon Marchi Date: Fri, 21 Nov 2025 20:13:59 +0000 (-0500) Subject: gdb/testsuite/dwarf: use single abbrev table in .dwo files X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6a29913eeb98595b76a7c5a3b3c2c4464c7c1484;p=thirdparty%2Fbinutils-gdb.git gdb/testsuite/dwarf: use single abbrev table in .dwo files When I wrote test gdb.dwarf2/fission-with-type-unit.exp, I did not use build_executable_and_dwo_files, because it wouldn't work to have multiple units in the .dwo file, each referring to their own abbrev table using labels. build_executable_and_dwo_files extracts the .dwo file content from the .o using objcopy (just like gcc does, I learned), meaning that the .dwo file never runs through a linker. Anything needing relocation (like labels pointing to abbrev tables) doesn't work. I instead opted to use gdb_compile_shlib to build the .dwo file on its own, so that those labels would get resolved. That causes problems now that I'm trying to write a test with multiple type units in a .dwo file, where each type unit should be in its own .debug_types section. Running the .dwo file through the linker causes all the .debug_types section to be collapsed into one. And generally, I think it was a bad idea to generate a .dwo file using the linker, since the idea behind .dwo files is that they do not need to be linked (therefore improving link times). We want to produce files as close to what an actual compiler would produce. This patch fixes this by doing what compilers do in the same situation: use a single abbrev table shared by all units in the .dwo file. This requires the following changes in lib/dwarf.exp: - Declare a new variable _dwo_abbrev_num, which holds the next abbrev number to use in the .dwo file's abbrev section (.debug_abbrev.dwo). Initialize this variable to 1. - When producing a CU or TU in a .dwo file, use 0 as the abbrev table offset. - When generating a DIE, return $_dwo_abbrev_num or $_abbrev_num, depending on whether the current CU is in a .dwo file. - After producing a CU or TU in a .dwo file, don't append the terminator byte. - After finishing producing the CUs and TUs, append the terminator byte in .debug_abbrev.dwo if we did output anything there. Update gdb.dwarf2/fission-with-type-unit.exp to use build_executable_and_dwo_files, as it should. Remove the gdb_remote_download call from gdb.dwarf/fission-with-type-unit.exp, because build_executable_and_dwo_files does not support remote hosts anyway. With this change, running with the cc-with-gdb-index board, I see: (gdb) maint expand-symtabs /home/smarchi/src/binutils-gdb/gdb/dwarf2/read.c:3056: internal-error: cutu_reader: Assertion `sig_type->signature == cu->header.signature' failed. A problem internal to GDB has been detected, further debugging may prove unreliable. ----- Backtrace ----- FAIL: gdb.dwarf2/fission-with-type-unit.exp: maint expand-symtabs (GDB internal error) This is actually an improvement, as the test case didn't run properly before. The compilation failed with: gdb compile failed, During symbol reading: Could not find DWO CU fission-with-type-unit.dwo(0xf00d) referenced by CU at offset 0xc [in module /home/smarchi/build/binutils-gdb/gdb/testsuite/outputs/gdb.dwarf2/fission-with-type-unit/.tmp/fission-with-type-unit] The reason was that the old code would try to generate the GDB index during this step: # Build main file. if { [build_executable "${testfile}.exp" $binfile \ [list ${srcfile} ${main_asm_file}] {nodebug}] } { return } ... which is before the DWO file is even generated. With this patch things are done in the correct order: - The -dw.S file is generated - The -dw.o file is compiled from the -dw.S - The .dwo sections are extracted to the .dwo file, and stripped from the -dw.o file - The executable is linked from the .o and -dw.o - gdb-add-index is ran on the executable When gdb-add-index runs, the .dwo file exists, so GDB is able to produce an index. That index is bogus though, because the .gdb_index format is unable to describe skeletonless type units. And then GDB gets confused trying to use that index, leading to the internal error. Change-Id: Iabbcf00db97faf2a4fa5fc71652ad273081189f9 Approved-By: Andrew Burgess --- diff --git a/gdb/testsuite/gdb.dwarf2/fission-with-type-unit.exp b/gdb/testsuite/gdb.dwarf2/fission-with-type-unit.exp index 58cda296c67..4b9de2e1911 100644 --- a/gdb/testsuite/gdb.dwarf2/fission-with-type-unit.exp +++ b/gdb/testsuite/gdb.dwarf2/fission-with-type-unit.exp @@ -21,25 +21,22 @@ load_lib dwarf.exp # This test can only be run on targets which support DWARF-2 and use gas. require dwarf2_support -standard_testfile .c -dw.S -dwo.S +standard_testfile .c -dw.S -set main_asm_file [standard_output_file $srcfile2] -set dwo_asm_file [standard_output_file $srcfile3] +set asm_file [standard_output_file $srcfile2] -# Debug info in the main file. -Dwarf::assemble $main_asm_file { +Dwarf::assemble $asm_file { + # In the main file. cu { version 5 dwo_id 0xF00D } { compile_unit { - DW_AT_dwo_name ${::gdb_test_file_name}.dwo DW_FORM_strp + DW_AT_dwo_name ${::gdb_test_file_name}-dw.dwo DW_FORM_strp } {} } -} -# Debug info in the DWO file. -Dwarf::assemble $dwo_asm_file { + # In the .dwo file. tu { fission 1 version 5 @@ -79,22 +76,13 @@ Dwarf::assemble $dwo_asm_file { } } -# Build main file. -if { [build_executable "${testfile}.exp" $binfile \ - [list ${srcfile} ${main_asm_file}] {nodebug}] } { - return -} - -# Build DWO file. -set dwo_file [standard_output_file ${testfile}.dwo] -if { [gdb_compile_shlib $dwo_asm_file $dwo_file nodebug] != "" } { +set obj [standard_output_file "${testfile}-dw.o"] +if {[build_executable_and_dwo_files "$testfile.exp" "${binfile}" {} \ + [list $asm_file {nodebug split-dwo} $obj] \ + [list $srcfile {nodebug}]]} { return } -if { [is_remote host] } { - gdb_remote_download host $dwo_file -} - clean_restart ${testfile} # This would cause an internal error. diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp index dc9d69518c5..cedc12aed57 100644 --- a/gdb/testsuite/lib/dwarf.exp +++ b/gdb/testsuite/lib/dwarf.exp @@ -566,6 +566,9 @@ namespace eval Dwarf { # table. variable _abbrev_num + # The next available abbrev number in the (single) DWO abbrev table. + variable _dwo_abbrev_num + # The string table for this assembly. The key is the string; the # value is the label for that string. variable _strings @@ -1011,8 +1014,17 @@ namespace eval Dwarf { # table. proc _get_abbrev_num {} { variable _abbrev_num - set res $_abbrev_num - incr _abbrev_num + variable _dwo_abbrev_num + variable _cu_is_fission + + if { $_cu_is_fission } { + set res $_dwo_abbrev_num + incr _dwo_abbrev_num + } else { + set res $_abbrev_num + incr _abbrev_num + } + return $res } @@ -1541,8 +1553,17 @@ namespace eval Dwarf { _section $section set cu_num [incr _cu_count] - set my_abbrevs [_compute_label "abbrev${cu_num}_begin"] - set _abbrev_num 1 + + # Since .dwo files are not linked, we can't use a label to point to a + # specific place in the .debug_abbrev section. For .dwo files, we + # therefore use a single abbrev table (at offset 0) shared by all units + # in that file. + if { $_cu_is_fission } { + set my_abbrevs 0 + } else { + set my_abbrevs [_compute_label "abbrev${cu_num}_begin"] + set _abbrev_num 1 + } set _cu_label [_compute_label "cu${cu_num}_begin"] set start_label [_compute_label "cu${cu_num}_start"] @@ -1609,9 +1630,11 @@ namespace eval Dwarf { uplevel $_level $body - _defer_output $_abbrev_section { - # Emit the terminator. - _op .byte 0x0 "Abbrev end - Terminator" + if { !$_cu_is_fission } { + _defer_output $_abbrev_section { + # Emit the terminator. + _op .byte 0x0 "Abbrev end - Terminator" + } } define_label $end_label @@ -1684,8 +1707,17 @@ namespace eval Dwarf { _section $section set cu_num [incr _cu_count] - set my_abbrevs [_compute_label "abbrev${cu_num}_begin"] - set _abbrev_num 1 + + # Since .dwo files are not linked, we can't use a label to point to a + # specific place in the .debug_abbrev section. For .dwo files, we + # therefore use a single abbrev table (at offset 0) shared by all units + # in that file. + if { $_cu_is_fission } { + set my_abbrevs 0 + } else { + set my_abbrevs [_compute_label "abbrev${cu_num}_begin"] + set _abbrev_num 1 + } set _cu_label [_compute_label "cu${cu_num}_begin"] set start_label [_compute_label "cu${cu_num}_start"] @@ -1739,9 +1771,11 @@ namespace eval Dwarf { uplevel $_level $body - _defer_output $_abbrev_section { - # Emit the terminator. - _op .byte 0x0 "Abbrev end - Terminator" + if { !$_cu_is_fission } { + _defer_output $_abbrev_section { + # Emit the terminator. + _op .byte 0x0 "Abbrev end - Terminator" + } } define_label $end_label @@ -3510,6 +3544,7 @@ namespace eval Dwarf { variable _debug_ranges_64_bit variable _debug_addr_index variable _level + variable _dwo_abbrev_num if { [llength $options] == 1 } { set options [list filename [lindex $options 0]] @@ -3542,8 +3577,8 @@ namespace eval Dwarf { set _line_count 0 set _debug_ranges_64_bit [is_64_target] - set _debug_addr_index 0 + set _dwo_abbrev_num 1 # Dummy CU at the start to ensure that the first CU in $body is not # the first in .debug_info. @@ -3565,6 +3600,14 @@ namespace eval Dwarf { dummy_cu } + # If we wrote any abbrev in .debug_abbrev.dwo, write the terminator. + if { $_dwo_abbrev_num > 1 } { + _defer_output .debug_abbrev.dwo { + # Emit the terminator. + _op .byte 0x0 "Abbrev end - Terminator" + } + } + _write_deferred_output _section .note.GNU-stack "" progbits