From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id EEF4B3856279 for ; Thu, 9 Jun 2022 12:39:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EEF4B3856279 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id D980A21F29; Thu, 9 Jun 2022 12:39:36 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id CD6DA2C141; Thu, 9 Jun 2022 12:39:36 +0000 (UTC) Received: by wotan.suse.de (Postfix, from userid 10510) id D7C4767F2; Thu, 9 Jun 2022 12:39:36 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by wotan.suse.de (Postfix) with ESMTP id D6D9167B1; Thu, 9 Jun 2022 12:39:36 +0000 (UTC) Date: Thu, 9 Jun 2022 12:39:36 +0000 (UTC) From: Michael Matz To: Carl Love cc: gcc@gcc.gnu.org Subject: Re: DWARF question about size of a variable In-Reply-To: <848a436ef86a2f3f12e291dcbbf58a89084a6053.camel@us.ibm.com> Message-ID: References: <848a436ef86a2f3f12e291dcbbf58a89084a6053.camel@us.ibm.com> User-Agent: Alpine 2.20 (LSU 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Jun 2022 12:39:39 -0000 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>: Abbrev Number: 12 (DW_TAG_variable) DW_AT_name : a DW_AT_type : <0xa29> So, 'a' is a variable of type 0xa29, which is: <1>: Abbrev Number: 13 (DW_TAG_array_type) DW_AT_type : <0xa4a> DW_AT_sibling : <0xa43> <2>: Abbrev Number: 14 (DW_TAG_subrange_type) DW_AT_type : <0xa43> 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>: 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>: Abbrev Number: 1 (DW_TAG_base_type) DW_AT_byte_size : 8 DW_AT_encoding : 7 (unsigned) DW_AT_name : (indirect string, offset: 0x13b): long unsigned int <1>: Abbrev Number: 1 (DW_TAG_base_type) DW_AT_byte_size : 1 DW_AT_encoding : 6 (signed char) 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>: Abbrev Number: 10 (DW_TAG_subrange_type) DW_AT_type : <0xa29> 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.