libdw: Use signedness of subrange type to determine array bounds

Message ID 20211006204129.29845-1-mark@klomp.org
State Committed
Headers
Series libdw: Use signedness of subrange type to determine array bounds |

Commit Message

Mark Wielaard Oct. 6, 2021, 8:41 p.m. UTC
  When calculating the array size check if the subrange has an associate
type, if it does then check the type to determine whether the upper
and lower values need to be interpreted as signed of unsigned
values. We default to signed because that is what the testcase
run-aggregate-size.sh testfile-size4 expects (this is an hardwritten
testcase, we could have chosen a different default).

https://sourceware.org/bugzilla/show_bug.cgi?id=28294

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdw/ChangeLog              |  6 +++++
 libdw/dwarf_aggregate_size.c | 44 +++++++++++++++++++++++++++++++-----
 2 files changed, 44 insertions(+), 6 deletions(-)
  

Patch

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index b707bbfe..4275b830 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,9 @@ 
+2021-10-06  Mark Wielaard  <mark@klomp.org>
+
+	* dwarf_aggregate_size.c (array_size): Check signedness of child DIE
+	type. Use dwarf_formsdata or dwarf_formudata to get the lower and
+	upper bounds.
+
 2021-09-08  Mark Wielaard  <mark@klomp.org>
 
 	* dwarf_begin_elf.c (valid_p): Identify ELF class and use this to set
diff --git a/libdw/dwarf_aggregate_size.c b/libdw/dwarf_aggregate_size.c
index 75105e4d..96023d69 100644
--- a/libdw/dwarf_aggregate_size.c
+++ b/libdw/dwarf_aggregate_size.c
@@ -83,19 +83,51 @@  array_size (Dwarf_Die *die, Dwarf_Word *size,
 	    }
 	  else
 	    {
+	      bool is_signed = true;
+	      if (INTUSE(dwarf_attr) (get_type (&child, attr_mem, &type_mem),
+				      DW_AT_encoding, attr_mem) != NULL)
+		{
+		  Dwarf_Word encoding;
+		  if (INTUSE(dwarf_formudata) (attr_mem, &encoding) == 0)
+		    is_signed = (encoding == DW_ATE_signed
+				 || encoding == DW_ATE_signed_char);
+		}
+
 	      Dwarf_Sword upper;
 	      Dwarf_Sword lower;
-	      if (INTUSE(dwarf_formsdata) (INTUSE(dwarf_attr_integrate)
-					   (&child, DW_AT_upper_bound,
-					    attr_mem), &upper) != 0)
-		return -1;
+	      if (is_signed)
+		{
+		  if (INTUSE(dwarf_formsdata) (INTUSE(dwarf_attr_integrate)
+					       (&child, DW_AT_upper_bound,
+						attr_mem), &upper) != 0)
+		    return -1;
+		}
+	      else
+		{
+		  Dwarf_Word unsigned_upper;
+		  if (INTUSE(dwarf_formudata) (INTUSE(dwarf_attr_integrate)
+					       (&child, DW_AT_upper_bound,
+						attr_mem), &unsigned_upper) != 0)
+		    return -1;
+		  upper = unsigned_upper;
+		}
 
 	      /* Having DW_AT_lower_bound is optional.  */
 	      if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_lower_bound,
 						attr_mem) != NULL)
 		{
-		  if (INTUSE(dwarf_formsdata) (attr_mem, &lower) != 0)
-		    return -1;
+		  if (is_signed)
+		    {
+		      if (INTUSE(dwarf_formsdata) (attr_mem, &lower) != 0)
+			return -1;
+		    }
+		  else
+		    {
+		      Dwarf_Word unsigned_lower;
+		      if (INTUSE(dwarf_formudata) (attr_mem, &unsigned_lower) != 0)
+			return -1;
+		      lower = unsigned_lower;
+		    }
 		}
 	      else
 		{