Hello,

On Wed, 8 Jun 2022, Carl Love via Gcc wrote:

> Is there dwarf information that gives the size of a variable?

Yes, it's in the type description.  For array types the siblings of it 
give the index types and ranges.  If that range is 
computed at runtime DWARF will (try to) express it as an expression in 
terms of other available values (like registers, constants, or memory), 
and as such can also change depending on where (at which PC) you evaluate 
that expression (and the expression itself can also change per PC).  For 
instance, in your example, on x86 with -O3 we have these relevant DWARF 
snippets (readelf -wi):

 <2><a1e>: Abbrev Number: 12 (DW_TAG_variable)
    <a1f>   DW_AT_name        : a
    <a24>   DW_AT_type        : <0xa29>

So, 'a' is a variable of type 0xa29, which is:

 <1><a29>: Abbrev Number: 13 (DW_TAG_array_type)
    <a2a>   DW_AT_type        : <0xa4a>
    <a2e>   DW_AT_sibling     : <0xa43>
 <2><a32>: Abbrev Number: 14 (DW_TAG_subrange_type)
    <a33>   DW_AT_type        : <0xa43>
    <a37>   DW_AT_upper_bound : 10 byte block: 75 1 8 20 24 8 20 26 31 1c       
(DW_OP_breg5 (rdi): 1; DW_OP_const1u: 32; DW_OP_shl; DW_OP_const1u: 32; 
DW_OP_shra; DW_OP_lit1; DW_OP_minus)
 <2><a42>: Abbrev Number: 0

So, type 0xa29 is an array type, whose element type is 0xa4a (which will 
turn out to be a signed char), and whose (single) dimension type is 0xa43 
(unsigned long) with an upper bound that is runtime computed, see below.
The referenced types from that are:

 <1><a43>: Abbrev Number: 1 (DW_TAG_base_type)
    <a44>   DW_AT_byte_size   : 8
    <a45>   DW_AT_encoding    : 7       (unsigned)
    <a46>   DW_AT_name        : (indirect string, offset: 0x13b): long unsigned 
int

 <1><a4a>: Abbrev Number: 1 (DW_TAG_base_type)
    <a4b>   DW_AT_byte_size   : 1
    <a4c>   DW_AT_encoding    : 6       (signed char)
    <a4d>   DW_AT_name        : (indirect string, offset: 0x1ce): char

With that gdb has all information to compute the size of this array 
variable in its scope ((upper-bound + 1 minus lower-bound (default 0)) 
times sizeof(basetype)).  Compare the above for instance with the 
debuginfo generated at -O0, only the upper-range expression changes:

 <2><a1f>: Abbrev Number: 10 (DW_TAG_subrange_type)
    <a20>   DW_AT_type        : <0xa29>
    <a24>   DW_AT_upper_bound : 3 byte block: 91 68 6   (DW_OP_fbreg: -24; 
DW_OP_deref)

Keep in mind that DWARF expressions are based on a simple stack machine.
So, for instance, the computation for the upper bound in the O3 case is:
 ((register %rdi + 1) << 32 >> 32) - 1
(i.e. basically the 32-to-64 signextension of %rdi).

On ppc I assume that either the upper_bound attribute isn't there or 
contains an uninformative expression (or one that isn't valid at the 
program-counter gdb stops at), in which case you would want to look at 
dwarf2out.cc:subrange_type_die or add_subscript_info (look for 
TYPE_MAX_VALUE of the subscripts domain type).  Hope this helps.


Ciao,
Michael.

Reply via email to