]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libdw: dwarf_aggregate_size() works with multi-dimensional arrays
authorDima Kogan <dkogan@debian.org>
Fri, 8 Dec 2017 09:45:10 +0000 (01:45 -0800)
committerMark Wielaard <mark@klomp.org>
Tue, 12 Dec 2017 09:39:57 +0000 (10:39 +0100)
If we have a multidimensional array of dimensions (a,b,c) the number of elements
should be a*b*c, but prior to this patch dwarf_aggregate_size() would report
a+b+c instead.

This patch fixes the bug and adds a test that demonstrates the bug (the test
fails without the functional part of this patch).

Fixes: https://sourceware.org/bugzilla/show_bug.cgi?id=22546
Signed-off-by: Dima Kogan <dima@secretsauce.net>
libdw/ChangeLog
libdw/dwarf_aggregate_size.c
tests/ChangeLog
tests/run-aggregate-size.sh
tests/run-peel-type.sh
tests/testfile-sizes3.o.bz2

index 4375244020c3a609027d7f06f1b165bf47f43ba4..2a6d711808f937557e9a6cb2f12df4258f477e43 100644 (file)
@@ -1,3 +1,8 @@
+2017-12-11  Dima Kogan  <dima@secretsauce.net>
+
+       * dwarf_aggregate_size.c (array_size): Handle multi-dimensional
+       arrays properly.
+
 2017-11-03  Mark Wielaard  <mark@klomp.org>
 
        * dwarf_getlocation.c (__libdw_intern_expression): Handle
index 838468dd9fbbbef6fa34a3806d930d45e1b0bcd5..3010c0aa2f3b436e5abe11fe52b1c9bb4b9f1393 100644 (file)
@@ -63,7 +63,7 @@ array_size (Dwarf_Die *die, Dwarf_Word *size,
     return -1;
 
   bool any = false;
-  Dwarf_Word total = 0;
+  Dwarf_Word count_total = 1;
   do
     {
       Dwarf_Word count;
@@ -134,34 +134,35 @@ array_size (Dwarf_Die *die, Dwarf_Word *size,
          continue;
        }
 
-      /* This is a subrange_type or enumeration_type and we've set COUNT.
-        Now determine the stride for this array dimension.  */
-      Dwarf_Word stride = eltsize;
-      if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_byte_stride,
-                                       attr_mem) != NULL)
-       {
-         if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
-           return -1;
-       }
-      else if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_bit_stride,
-                                            attr_mem) != NULL)
-       {
-         if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
-           return -1;
-         if (stride % 8)       /* XXX maybe compute in bits? */
-           return -1;
-         stride /= 8;
-       }
+      count_total *= count;
 
       any = true;
-      total += stride * count;
     }
   while (INTUSE(dwarf_siblingof) (&child, &child) == 0);
 
   if (!any)
     return -1;
 
-  *size = total;
+  /* This is a subrange_type or enumeration_type and we've set COUNT.
+     Now determine the stride for this array.  */
+  Dwarf_Word stride = eltsize;
+  if (INTUSE(dwarf_attr_integrate) (die, DW_AT_byte_stride,
+                                    attr_mem) != NULL)
+    {
+      if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
+        return -1;
+    }
+  else if (INTUSE(dwarf_attr_integrate) (die, DW_AT_bit_stride,
+                                         attr_mem) != NULL)
+    {
+      if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
+        return -1;
+      if (stride % 8)  /* XXX maybe compute in bits? */
+        return -1;
+      stride /= 8;
+    }
+
+  *size = count_total * stride;
   return 0;
 }
 
index fe63359447fdac8c96e5964f376df933a3c71338..e16a3d0479abe6b3b37dfbbd45cb5476d04d442b 100644 (file)
@@ -1,3 +1,9 @@
+2017-12-11  Dima Kogan  <dima@secretsauce.net>
+
+        * run-aggregate-size.sh: Added check for multi-dimensional arrays.
+        * run-peel-type.sh: Likewise.
+        * testfile-sizes3.o.bz2: Likewise.
+
 2017-12-07  Mark Wielaard  <mark@klomp.org>
 
        * run-readelf-variant.sh: New test.
index 42b0742b126563039cbee3bb29edc69e14153a17..6d8aa240187c697207656bb8e279b39957ab0f60 100755 (executable)
@@ -54,6 +54,7 @@
 # volatile int ia[32];
 # const volatile void * const volatile restrict va[64];
 # struct s sa[8];
+# double d3d[3][4][5];
 #
 # typedef const int foo;
 # typedef volatile foo bar;
@@ -98,6 +99,7 @@ ca size 16
 ia size 128
 va size 512
 sa size 128
+d3d size 480
 f size 4
 b size 4
 EOF
index 7fd96e84be574246e903093d55fcc7b50675cbe8..668e31617650d37d56a7b4402ae18a5ea13c71ae 100755 (executable)
@@ -55,6 +55,7 @@ ca raw type array_type
 ia raw type array_type
 va raw type array_type
 sa raw type array_type
+d3d raw type array_type
 f raw type base_type
 b raw type base_type
 EOF
index 7fa6a8a529676bc8324d383c8d11f86c66ef453c..863338269bab61fb385344c489d48d1466915b61 100644 (file)
Binary files a/tests/testfile-sizes3.o.bz2 and b/tests/testfile-sizes3.o.bz2 differ