Bug 29559

Summary: gas generated incorrect debug info (top-level DW_TAG_unspecified_type DIE)
Product: binutils Reporter: Tom de Vries <vries>
Component: gasAssignee: Nick Clifton <nickc>
Status: RESOLVED FIXED    
Severity: normal CC: nickc
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Attachments: Dwarf assembly test-case

Description Tom de Vries 2022-09-08 09:00:53 UTC
See the test-case in gdb PR29558.

Current trunk gas generates a top-level DIE with DW_TAG_unspecified_type:
...
  Compilation Unit @ offset 0xf4:
   Length:        0x35 (32-bit)
   Version:       5
   Unit Type:     DW_UT_compile (1)
   Abbrev Offset: 0xa3
   Pointer Size:  8
 <0><100>: Abbrev Number: 3 (DW_TAG_unspecified_type)
 <0><101>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <102>   DW_AT_stmt_list   : 0x123
    <106>   DW_AT_low_pc      : 0x4004ac
    <10e>   DW_AT_high_pc     : 11
    <10f>   DW_AT_name        : test2.s
    <113>   DW_AT_comp_dir    : /home/vries/gdb_versions/devel
    <117>   DW_AT_producer    : GNU AS 2.39.50
    <11b>   DW_AT_language    : 32769   (MIPS assembler)
 <1><11d>: Abbrev Number: 2 (DW_TAG_subprogram)
    <11e>   DW_AT_name        : (indirect string, offset: 0x241): foo
    <122>   DW_AT_external    : 1
    <122>   DW_AT_type        : <0x100>
    <123>   DW_AT_low_pc      : 0x4004ac
    <12b>   DW_AT_high_pc     : 11
...

This is incorrect dwarf, according to the standard (see PR29558 comment 3).
Comment 1 Nick Clifton 2022-09-08 09:19:29 UTC
Hi Tom,

  [For the record the patch that introduced this bug was intended to fix PR 29517].

>  <0><100>: Abbrev Number: 3 (DW_TAG_unspecified_type)
>  <0><101>: Abbrev Number: 1 (DW_TAG_compile_unit)
 
OK, so you are saying that the DW_TAG_unspecified_type is in the wrong place ?

Presumably it should be after DW_TAG_subprogram.  IE, something like this:

 <0><101>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <102>   DW_AT_stmt_list   : 0x123
    <106>   DW_AT_low_pc      : 0x4004ac
    <10e>   DW_AT_high_pc     : 11
    <10f>   DW_AT_name        : test2.s
    <113>   DW_AT_comp_dir    : /home/vries/gdb_versions/devel
    <117>   DW_AT_producer    : GNU AS 2.39.50
    <11b>   DW_AT_language    : 32769   (MIPS assembler)
 <1><11d>: Abbrev Number: 2 (DW_TAG_subprogram)
    <11e>   DW_AT_name        : (indirect string, offset: 0x241): foo
    <122>   DW_AT_external    : 1
    <122>   DW_AT_type        : <0x133>
    <123>   DW_AT_low_pc      : 0x4004ac
    <12b>   DW_AT_high_pc     : 11
 <1><133>: Abbrev Number: 3 (DW_TAG_unspecified_type)

I will try to fix this.  I originally placed the unspecified tag at the start of the CU information because I found that I could not otherwise compute the correct offset for it.  I will just have to try harder.

Cheers
  Nick
Comment 2 Tom de Vries 2022-09-08 09:24:03 UTC
(In reply to Nick Clifton from comment #1)
> Hi Tom,
> 
>   [For the record the patch that introduced this bug was intended to fix PR
> 29517].
> 
> >  <0><100>: Abbrev Number: 3 (DW_TAG_unspecified_type)
> >  <0><101>: Abbrev Number: 1 (DW_TAG_compile_unit)
>  
> OK, so you are saying that the DW_TAG_unspecified_type is in the wrong place
> ?
> 

Yes.

> Presumably it should be after DW_TAG_subprogram.  IE, something like this:
> 
>  <0><101>: Abbrev Number: 1 (DW_TAG_compile_unit)
>     <102>   DW_AT_stmt_list   : 0x123
>     <106>   DW_AT_low_pc      : 0x4004ac
>     <10e>   DW_AT_high_pc     : 11
>     <10f>   DW_AT_name        : test2.s
>     <113>   DW_AT_comp_dir    : /home/vries/gdb_versions/devel
>     <117>   DW_AT_producer    : GNU AS 2.39.50
>     <11b>   DW_AT_language    : 32769   (MIPS assembler)
>  <1><11d>: Abbrev Number: 2 (DW_TAG_subprogram)
>     <11e>   DW_AT_name        : (indirect string, offset: 0x241): foo
>     <122>   DW_AT_external    : 1
>     <122>   DW_AT_type        : <0x133>
>     <123>   DW_AT_low_pc      : 0x4004ac
>     <12b>   DW_AT_high_pc     : 11
>  <1><133>: Abbrev Number: 3 (DW_TAG_unspecified_type)
> 
> I will try to fix this.  I originally placed the unspecified tag at the
> start of the CU information because I found that I could not otherwise
> compute the correct offset for it.  I will just have to try harder.

After the DW_TAG_compile_unit should also be fine.
Comment 3 Tom de Vries 2022-09-08 10:43:08 UTC
Created attachment 14325 [details]
Dwarf assembly test-case


Currently, this fails:
...
(gdb) PASS: gdb.dwarf2/dw2-unspecified-type.exp: p ((int (*) ()) foo) ()
p (int) foo ()^M
Invalid cast.^M
(gdb) FAIL: gdb.dwarf2/dw2-unspecified-type.exp: p (int) foo ()
...
because DW_TAG_unspecified_type is translated as void.
Comment 4 Tom de Vries 2022-09-08 10:43:27 UTC
(In reply to Tom de Vries from comment #3)
> Created attachment 14325 [details]
> Dwarf assembly test-case
> 
> 
> Currently, this fails:
> ...
> (gdb) PASS: gdb.dwarf2/dw2-unspecified-type.exp: p ((int (*) ()) foo) ()
> p (int) foo ()^M
> Invalid cast.^M
> (gdb) FAIL: gdb.dwarf2/dw2-unspecified-type.exp: p (int) foo ()
> ...
> because DW_TAG_unspecified_type is translated as void.

Sorry, meant for the gdb PR.
Comment 5 Sourceware Commits 2022-09-08 11:44:07 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

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

commit 0ee31dffb8202dcd1f867b7144db9a8bd69a7499
Author: Nick Clifton <nickc@redhat.com>
Date:   Thu Sep 8 12:43:33 2022 +0100

    Gas generated incorrect debug info (top-level DW_TAG_unspecified_type DIE)
    
            PR 29559
            * dwarf2dbg.c (out_debug_info): Place DW_TAG_unspecified_type at
            the end of the list of children, not at the start of the CU
            information.
            * testsuite/gas/elf/dwarf-3-func.d: Update expected output.
            * testsuite/gas/elf/dwarf-5-func-global.d: Likewise.
            * testsuite/gas/elf/dwarf-5-func-local.d: Likewise.
            * testsuite/gas/elf/dwarf-5-func.d: Likewise.
Comment 6 Nick Clifton 2022-09-08 11:44:40 UTC
Right - I have applied a patch which should fix this problem.
Comment 7 Tom de Vries 2022-09-08 12:28:18 UTC
Ack, FWIW, updated DWARF:
...
 <0><100>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <101>   DW_AT_stmt_list   : 0x123
    <105>   DW_AT_low_pc      : 0x4004ac
    <10d>   DW_AT_high_pc     : 11
    <10e>   DW_AT_name        : (indirect string, offset: 0x22a): test2.s
    <112>   DW_AT_comp_dir    : (indirect string, offset: 0x1d1): /home/vries/gdb_versions/dev
el
    <116>   DW_AT_producer    : (indirect string, offset: 0x232): GNU AS 2.39.50
    <11a>   DW_AT_language    : 32769   (MIPS assembler)
 <1><11c>: Abbrev Number: 2 (DW_TAG_subprogram)
    <11d>   DW_AT_name        : (indirect string, offset: 0x241): foo
    <121>   DW_AT_external    : 1
    <121>   DW_AT_type        : <0x12b>
    <122>   DW_AT_low_pc      : 0x4004ac
    <12a>   DW_AT_high_pc     : 11
 <1><12b>: Abbrev Number: 3 (DW_TAG_unspecified_type)
 <1><12c>: Abbrev Number: 0
...