Bug 101643

Summary: [debug, ada] packed array not described as packed
Product: gcc Reporter: Tom de Vries <vries>
Component: debugAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal Keywords: wrong-debug
Priority: P3    
Version: 8.5.0   
Target Milestone: 9.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Tom de Vries 2021-07-27 14:15:03 UTC
[ Filing FTR. ]

Consider gdb test-case foo_ra24_010.adb / pck.adb / pck.ads ( https://sourceware.org/git/?p=binutils-gdb.git;a=tree;f=gdb/testsuite/gdb.ada/big_packed_array ).

With gcc-8.5.0 we have:
...
$ gnatmake-8 -f foo_ra24_010.adb -fgnat-encodings=minimal -g
$ gdb -q -batch ./foo_ra24_010 -ex start -ex n -ex n -ex 'p bad'
Temporary breakpoint 1 at 0x402ce6: file foo_ra24_010.adb, line 19.

Temporary breakpoint 1, foo_ra24_010 () at foo_ra24_010.adb:19
19         Good : PA := (others => False);
20         Bad : Bad_Packed_Table := (others => False);
22         Do_Nothing (Good'Address);  -- STOP
$1 = (0 => 0 <repeats 25 times>)
...
while with gcc-9.3.1 we have:
...
$ gnatmake-9 -f foo_ra24_010.adb -fgnat-encodings=minimal -g
+ gdb -q -batch ./foo_ra24_010 -ex start -ex n -ex n -ex 'p bad'
Temporary breakpoint 1 at 0x402d24: file foo_ra24_010.adb, line 19.

Temporary breakpoint 1, foo_ra24_010 () at foo_ra24_010.adb:19
19         Good : PA := (others => False);
20         Bad : Bad_Packed_Table := (others => False);
22         Do_Nothing (Good'Address);  -- STOP
$1 = (false <repeats 196 times>)
...

The difference in gdb behaviour is caused by the dwarf information.

With gcc-8.5.0 we have:
...
 <2><2074>: Abbrev Number: 10 (DW_TAG_variable)
    <2075>   DW_AT_name        : bad
    <207c>   DW_AT_type        : <0x2028>
 <1><2028>: Abbrev Number: 5 (DW_TAG_array_type)
    <2029>   DW_AT_name        : pck__t
    <202d>   DW_AT_bit_stride  : 8
    <202e>   DW_AT_type        : <0x2003>
    <2032>   DW_AT_alignment   : 1
    <2033>   DW_AT_sibling     : <0x203f>
 <2><2037>: Abbrev Number: 7 (DW_TAG_subrange_type)
    <2038>   DW_AT_type        : <0x200a>
    <203c>   DW_AT_lower_bound : 0
    <203d>   DW_AT_upper_bound : 24
 <2><203e>: Abbrev Number: 0
 <1><2003>: Abbrev Number: 4 (DW_TAG_base_type)
    <2004>   DW_AT_byte_size   : 1
    <2005>   DW_AT_encoding    : 7      (unsigned)
    <2006>   DW_AT_name        : system__unsigned_types__packed_byte
 <1><200a>: Abbrev Number: 4 (DW_TAG_base_type)
    <200b>   DW_AT_byte_size   : 4
    <200c>   DW_AT_encoding    : 5      (signed)
    <200d>   DW_AT_name        : integer
...

With gcc-9.3.1 we have for foo_ra24_010.adb:
...
 <2><20a1>: Abbrev Number: 11 (DW_TAG_variable)
    <20a2>   DW_AT_name        : bad
    <20a9>   DW_AT_type        : <0x205f>
 <1><205f>: Abbrev Number: 7 (DW_TAG_typedef)
    <2060>   DW_AT_name        : pck__bad_packed_table
    <2067>   DW_AT_type        : <0x2048>
    <206b>   DW_AT_alignment   : 1
 <1><2048>: Abbrev Number: 5 (DW_TAG_array_type)
    <2049>   DW_AT_name        : pck__t___XP1
    <204d>   DW_AT_bit_stride  : 8
    <204e>   DW_AT_type        : <0x2016>
    <2052>   DW_AT_alignment   : 1
 <2><2057>: Abbrev Number: 8 (DW_TAG_subrange_type)
    <2058>   DW_AT_type        : <0x201d>
    <205c>   DW_AT_lower_bound : 0
    <205d>   DW_AT_upper_bound : 24
 <2><205e>: Abbrev Number: 0
 <1><2016>: Abbrev Number: 4 (DW_TAG_base_type)
    <2017>   DW_AT_byte_size   : 1
    <2018>   DW_AT_encoding    : 7      (unsigned)
    <2019>   DW_AT_name        : system__unsigned_types__packed_byte
 <1><201d>: Abbrev Number: 4 (DW_TAG_base_type)
    <201e>   DW_AT_byte_size   : 4
    <201f>   DW_AT_encoding    : 5      (signed)
    <2020>   DW_AT_name        : integer
...
and with pck__t___XP1 listed once more for pck.adb as:
...
 <1><1ab5>: Abbrev Number: 5 (DW_TAG_array_type)
    <1ab6>   DW_AT_name        : pck__t___XP1
    <1aba>   DW_AT_bit_stride  : 1
    <1abb>   DW_AT_type        : <0x1aa0>
    <1abf>   DW_AT_alignment   : 1
 <2><1ac4>: Abbrev Number: 6 (DW_TAG_subrange_type)
    <1ac5>   DW_AT_type        : <0x15f9>
    <1ac9>   DW_AT_lower_bound : 0
    <1aca>   DW_AT_upper_bound : 195
 <2><1acb>: Abbrev Number: 0
 <1><1aa0>: Abbrev Number: 4 (DW_TAG_base_type)
    <1aa1>   DW_AT_byte_size   : 1
    <1aa2>   DW_AT_encoding    : 2      (boolean)
    <1aa3>   DW_AT_name        : boolean
 <1><15f9>: Abbrev Number: 2 (DW_TAG_enumeration_type)
    <15fa>   DW_AT_name        : pck__enum_idx
    <15fe>   DW_AT_encoding    : 7      (unsigned)
    <15ff>   DW_AT_byte_size   : 1
...

ISTM that in the gcc-8.5.0 case, gdb does the best it can do with the info provided, and that is this a gcc bug.
Comment 1 Tom de Vries 2021-07-27 14:17:27 UTC
Fixed by:
...
commit 21afc4facd1ac4f62f7bd8ca0a3997fdb8ed1230
Author:     Eric Botcazou <ebotcazou@adacore.com>
AuthorDate: Thu May 31 10:46:02 2018 +0000
Commit:     Pierre-Marie de Rodat <pmderodat@gcc.gnu.org>
CommitDate: Thu May 31 10:46:02 2018 +0000

    [Ada] Fix strange behavior of Object_Size for packed array subtype
    
    2018-05-31  Eric Botcazou  <ebotcazou@adacore.com>
    
    gcc/ada/
    
            * gcc-interface/decl.c (gnat_to_gnu_entity) <discrete_type>: Do not
            deal with the ___XP suffix for packed array types here...
            <E_Array_Subtype>: ...or here when processing the implementation type
            but when processing the original type instead.  Do not reuse the DECL
            of the implementation type for the original type.  Tidy up.
    
    From-SVN: r260999
...

I was not able to find a submission of this patch.