Bug 24550 - eu-readelf does not know about DW_AT_GNU_{bias,numerator,denumerator}
Summary: eu-readelf does not know about DW_AT_GNU_{bias,numerator,denumerator}
Status: RESOLVED FIXED
Alias: None
Product: elfutils
Classification: Unclassified
Component: tools (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-13 17:54 UTC by Tom Tromey
Modified: 2019-05-16 15:28 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Tromey 2019-05-13 17:54:27 UTC
The Ada compiler can emit DW_AT_GNU_bias in some situations.

Here is "tt.adb":

with Text_IO; use Text_IO;
procedure Tt is
   type Small is range -7 .. -4;
   for Small'Size use 2;
   Y : Small := -5;

  type Repeat_Count_T is range 1 .. 2 ** 6;
  for Repeat_Count_T'Size use 6;
  X : Repeat_Count_T := 64;
begin
  Put_Line (X'Image);
  Put_Line (Y'Image);
end Tt;


Compile with

gcc -fgnat-encodings=minimal -g -c tt.adb

gdb's dwarf2.def says:

/* Biased integer extension.
   See https://gcc.gnu.org/wiki/DW_AT_GNU_bias .  */
DW_TAG (DW_AT_GNU_bias, 0x2305)
Comment 1 Mark Wielaard 2019-05-14 18:36:46 UTC
We are also missing GNU_AT_GNU_numerator and GNU_AT_GNU_denominator.

In theory all that is needed is add them to dwarf.h.
Since they are simple constant attributes.

diff --git a/libdw/dwarf.h b/libdw/dwarf.h
index dc5973352..71ca2baae 100644
--- a/libdw/dwarf.h
+++ b/libdw/dwarf.h
@@ -351,6 +351,12 @@ enum
     DW_AT_GNU_pubnames = 0x2134,
     DW_AT_GNU_pubtypes = 0x2135,
 
+    /* https://gcc.gnu.org/wiki/DW_AT_GNU_numerator_denominator  */
+    DW_AT_GNU_numerator = 0x2303,
+    DW_AT_GNU_denominator = 0x2304,
+    /* https://gcc.gnu.org/wiki/DW_AT_GNU_bias  */
+    DW_AT_GNU_bias = 0x2305,
+
     DW_AT_hi_user = 0x3fff
   };
 
But I want to make sure we get the type/signedness right.

Could you help me with creating gnat examples that compile and show their usage?

If I try to compile your example I get two warnings:

tt.adb:4:04: warning: size clause forces biased representation for "Small"
tt.adb:8:03: warning: size clause forces biased representation for "Repeat_Count_T"

Are those expected?

Also the subranges come out as:

 [    4e]      subrange_type        abbrev: 3
               lower_bound          (sdata) -7
               upper_bound          (sdata) -4
               GNU_bias             (sdata) -7
               name                 (strp) "tt__small"
               type                 (ref4) [    8d]
 [    8d]    base_type            abbrev: 7
             byte_size            (data1) 1
             encoding             (data1) signed (5)
             name                 (strp) "tt__TsmallB"
             artificial           (flag_present) yes

and

 [    6d]      subrange_type        abbrev: 6
               upper_bound          (sdata) 64
               GNU_bias             (sdata) 1
               name                 (strp) "tt__repeat_count_t"
               type                 (ref4) [    94]
 [    94]    base_type            abbrev: 7
             byte_size            (data1) 1
             encoding             (data1) signed (5)
             name                 (strp) "tt__Trepeat_count_tB"
             artificial           (flag_present) yes

There are three things that surprised me about this.
First that the byte_size is 1, are these types really using a full byte and not 2 or 6 bits?
And second that the bias is on the subrange_type and not the base_type. Is that intended?
Finally, why is the last type signed?

Also could you help me create an compilable example of https://gcc.gnu.org/wiki/DW_AT_GNU_numerator_denominator

Thanks,

Mark
Comment 2 Tom Tromey 2019-05-15 16:13:52 UTC
(In reply to Mark Wielaard from comment #1)

> Could you help me with creating gnat examples that compile and show their
> usage?

For bias, the above works.

> If I try to compile your example I get two warnings:
> 
> tt.adb:4:04: warning: size clause forces biased representation for "Small"
> tt.adb:8:03: warning: size clause forces biased representation for
> "Repeat_Count_T"
> 
> Are those expected?

Yes, though I don't know the rationale.

The numerator and denominator attributes are used for fixed-point types.
There is a test for this in git@github.com:pmderodat/dwarf-ada-testsuite.git

murgatroyd. readelf -wi tests/fixedpoint/foo.o |grep DW_AT_GNU_[dn]
    <229>   DW_AT_GNU_numerator: 1
    <22a>   DW_AT_GNU_denominator: 30
    <237>   DW_AT_GNU_numerator: 0
    <238>   DW_AT_GNU_denominator: 0


> Also the subranges come out as:
[...]
> There are three things that surprised me about this.
> First that the byte_size is 1, are these types really using a full byte and
> not 2 or 6 bits?

I'm not totally sure what is going on here.  Maybe the type needs to be
used in a packed record to see it actually shrink?  I will see if I can write
a test for this.

> And second that the bias is on the subrange_type and not the base_type. Is
> that intended?

Yes, the bias only applies to subrange types, not the base type.

> Finally, why is the last type signed?

I think that's the default.  You have to request an unsigned based type explicitly, like:

   type Byte is mod 256;
   type Repeat_Count_T is new Byte range 1 .. 2 ** 6;
Comment 3 Tom Tromey 2019-05-15 18:02:35 UTC
> > There are three things that surprised me about this.
> > First that the byte_size is 1, are these types really using a full byte and
> > not 2 or 6 bits?
> 
> I'm not totally sure what is going on here.  Maybe the type needs to be
> used in a packed record to see it actually shrink?  I will see if I can write
> a test for this.

Thanks for noticing this.  I think the DWARF is incorrect here in a few
ways -- I am going to update the internal bug about it to get it fixed.

Meanwhile here is the updated test case I am using

--  Copyright 2019 Free Software Foundation, Inc.
--
--  This program is free software; you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation; either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.

procedure Bias is
   type Small is range -7 .. -4;
   for Small'Size use 2;
   Y : Small := -5;
   Y1 : Small := -7;

   type Byte is mod 256;
   type Repeat_Count_T is new Byte range 1 .. 2 ** 6;
   for Repeat_Count_T'Size use 6;
   X : Repeat_Count_T := 64;
   X1 : Repeat_Count_T := 1;

   type Char_Range is range 65 .. 68;
   for Char_Range'Size use 2;
   Cval : Char_Range := 65;

   type SomePackedRecord is record
      R: Small;
      S: Small;
   end record;
   pragma Pack (SomePackedRecord);
   SPR : SomePackedRecord := (R => -4, S => -5);

   type Packed_Array is array (1 .. 3) of Small;
   pragma pack (Packed_Array);
   A : Packed_Array := (-7, -5, -4);

begin
   null;
end Bias;
Comment 4 Tom Tromey 2019-05-15 18:17:26 UTC
BTW some of the oddities encountered might be good for dwarflint...
Comment 5 Mark Wielaard 2019-05-16 15:17:22 UTC
Thanks for the analysis. For now I'll just go with adding the constants. They seem to be printed as expected. But having examples in the testsuite might not be the right time now since you might still fix some things about the actual DWARF emitted.
Comment 6 Mark Wielaard 2019-05-16 15:28:52 UTC
commit a117891a00507d188fdab415bd25554ace9ed7ba
Author: Mark Wielaard <mark@klomp.org>
Date:   Thu May 16 17:20:35 2019 +0200

    libdw: Add DW_AT_GNU_numerator, DW_AT_GNU_denominator and DW_AT_GNU_bias.
    
    https://sourceware.org/bugzilla/show_bug.cgi?id=24550
    
    Signed-off-by: Mark Wielaard <mark@klomp.org>