Bug 32223 - [gdb/symtab] Support object files with multiple .debug_info sections
Summary: [gdb/symtab] Support object files with multiple .debug_info sections
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 enhancement
Target Milestone: 16.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-09-29 07:50 UTC by Tom de Vries
Modified: 2024-10-29 09:11 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2024-09-29 07:50:56 UTC

    
Comment 1 Tom de Vries 2024-09-29 07:55:00 UTC
Gdb can create inferiors based on object files.

With -gdwarf-4 -fdebug-types-section, we get multiple .debug_types sections, which gdb deals with, see create_all_units:
...
  for (dwarf2_section_info &section : per_objfile->per_bfd->types)
    read_comp_units_from_section (per_objfile, &section,
                                  &per_objfile->per_bfd->abbrev, 0,
                                  types_htab, rcuh_kind::TYPE);
...

However, with -gdwarf-5 -fdebug-types-section, we get multiple .debug_info sections, and that's not supported currently.
Comment 2 Tom de Vries 2024-09-29 07:58:19 UTC
Demonstrator:
...
$ cat test.c
namespace sp1 {
  class A {
    int i;
    const int c1 = 1;
    const int c2 = 2;
    const int c3 = 3;
    const int c4 = 4;
    const int c5 = 5;
    const int c6 = 6;
  };

}

sp1::A a;

int
main (void)
{
  return 0;
}
$ g++ test.c -g -gdwarf-5 -fdebug-types-section -S
$ fgrep ".section" test.s
	.section	.text._ZN3sp11AC2Ev,"axG",@progbits,_ZN3sp11AC5Ev,comdat
	.section	.init_array,"aw"
	.section	.debug_info,"G",@progbits,wi.bd7e4932b380e10c,comdat
	.section	.debug_info,"",@progbits
	.section	.debug_abbrev,"",@progbits
	.section	.debug_aranges,"",@progbits
	.section	.debug_rnglists,"",@progbits
	.section	.debug_line,"",@progbits
	.section	.debug_str,"MS",@progbits,1
	.section	.debug_line_str,"MS",@progbits,1
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
...
Comment 3 Tom de Vries 2024-09-29 08:06:12 UTC
Demonstrator using gdb:
...
$ g++ test.c -g -gdwarf-5 -fdebug-types-section -c
$ gdb -q -batch test.o -ex "ptype a"
'a' has unknown type; cast it to its declared type
$ g++ test.c -g -gdwarf-4 -fdebug-types-section -c
$ gdb -q -batch test.o -ex "ptype a"
type = class sp1::A {
    int i;
    const int c1;
    const int c2;
    const int c3;
    const int c4;
    const int c5;
    const int c6;
}
...
Comment 4 Tom de Vries 2024-09-29 16:04:18 UTC
To trigger this in the testsuite: run test-case gdb.cp/static-print-quit.exp with target board debug-types, with a compiler defaulting to DWARF version 5, or edit the board file to force that version.
Comment 6 Sourceware Commits 2024-10-29 09:08:23 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=80ac47851105702689004e07952ab6a4c04062b0

commit 80ac47851105702689004e07952ab6a4c04062b0
Author: Tom de Vries <tdevries@suse.de>
Date:   Tue Oct 29 10:08:04 2024 +0100

    [gdb/symtab] Handle multiple .debug_info sections
    
    When compiling dw2-multiple-debug-info.c using -gdwarf-5
    -fdebug-types-section, we end with two .debug_info sections in the object
    file:
    ...
    $ g++ gdb.dwarf2/dw2-multiple-debug-info.c -c -g \
        -gdwarf-5 \
        -fdebug-types-section
    $ readelf -WS dw2-multiple-debug-info.o | grep -v RELA | grep .debug_info
      [10] .debug_info  PROGBITS        0 000128 0000cd 00  GC  0   0  8
      [12] .debug_info  PROGBITS        0 0001f8 0000ad 00   C  0   0  8
    ...
    
    One of them contains the CU for dw2-multiple-debug-info.c, the other contains
    the TU for the type of variable a.
    
    When trying to print the type of variable a, we get:
    ...
    $ gdb -q -batch dw2-multiple-debug-info.o -ex "ptype a"
    'a' has unknown type; cast it to its declared type
    ...
    because the TU hasn't been read.
    
    Fix this by adding support for reading multiple .debug_info sections, similar
    to how that is done for multiple .debug_types sections, getting us instead:
    ...
    $ gdb -q -batch dw2-multiple-debug-info.o -ex "ptype a"
    type = class sp1::A {
      ...
    }
    ...
    
    Tested on x86_64-linux.
    
    PR symtab/32223
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32223
Comment 7 Tom de Vries 2024-10-29 09:11:13 UTC
Fixed.