public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Michael Matz <matz@suse.de>
To: Carl Love <cel@us.ibm.com>
Cc: gcc@gcc.gnu.org
Subject: Re: DWARF question about size of a variable
Date: Thu, 9 Jun 2022 12:39:36 +0000 (UTC)	[thread overview]
Message-ID: <alpine.LSU.2.20.2206091141580.9763@wotan.suse.de> (raw)
In-Reply-To: <848a436ef86a2f3f12e291dcbbf58a89084a6053.camel@us.ibm.com>

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.

      parent reply	other threads:[~2022-06-09 12:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-08 19:58 Carl Love
2022-06-08 22:57 ` Eric Botcazou
2022-06-09 12:39 ` Michael Matz [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LSU.2.20.2206091141580.9763@wotan.suse.de \
    --to=matz@suse.de \
    --cc=cel@us.ibm.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).