]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libdw: dwarf_aggregate_size return CU address_size for sizeless pointer/refs.
authorMark Wielaard <mjw@redhat.com>
Thu, 2 Oct 2014 12:00:47 +0000 (14:00 +0200)
committerMark Wielaard <mjw@redhat.com>
Thu, 2 Oct 2014 20:58:12 +0000 (22:58 +0200)
Tested-by: Conrad Meyer <cse.cem@gmail.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
libdw/ChangeLog
libdw/dwarf_aggregate_size.c
tests/ChangeLog
tests/Makefile.am
tests/aggregate_size.c [new file with mode: 0644]
tests/run-aggregate-size.sh [new file with mode: 0755]
tests/testfile-sizes1.o.bz2 [new file with mode: 0644]
tests/testfile-sizes2.o.bz2 [new file with mode: 0644]

index 410b31ab0709fefe877d4e8113674a6e52bc8545..29edc46e8050cf1319b412d0ee32cea05ee45b4b 100644 (file)
@@ -1,3 +1,9 @@
+2014-10-02  Mark Wielaard  <mjw@redhat.com>
+
+       * dwarf_aggregate_size.c (aggregate_size): Return CU address_size
+       for sizeless DW_TAG_pointer_type, DW_TAG_reference_type or
+       DW_TAG_rvalue_reference_type.
+
 2014-09-12  Petr Machata  <pmachata@redhat.com>
 
        * memory-access.h (read_ubyte_unaligned_inc): Allow only 4- and
index 07c53a22368d196f7aa4e93845da5cd9ccc11b46..5d23541e9b4e3bff310410675b64874d7a3cf4f4 100644 (file)
@@ -1,5 +1,5 @@
 /* Compute size of an aggregate type from DWARF.
-   Copyright (C) 2010 Red Hat, Inc.
+   Copyright (C) 2010, 2014 Red Hat, Inc.
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -205,6 +205,14 @@ aggregate_size (Dwarf_Die *die, Dwarf_Word *size, Dwarf_Die *type_mem)
 
     case DW_TAG_array_type:
       return array_size (die, size, &attr_mem, type_mem);
+
+    /* Assume references and pointers have pointer size if not given an
+       explicit DW_AT_byte_size.  */
+    case DW_TAG_pointer_type:
+    case DW_TAG_reference_type:
+    case DW_TAG_rvalue_reference_type:
+      *size = die->cu->address_size;
+      return 0;
     }
 
   /* Most types must give their size directly.  */
index 96320fa13c93d3c87f5c571ae23a450e1d50915f..a6111afee2bf0c15806d2e54d35a54ca08a1ffc8 100644 (file)
@@ -1,3 +1,15 @@
+2014-10-02  Mark Wielaard  <mjw@redhat.com>
+
+       * Makefile.am (check_PROGRAMS): Add aggregate_size.c.
+       (TESTS): Add run-aggregate-size.sh.
+       (EXTRA_DIST): Add run-aggregate-size.sh, testfile-sizes1.o.bz2
+       and testfile-sizes2.o.bz2.
+       (aggregate_size_LDADD): New variable.
+       * aggregate_size.c: New file.
+       * run-aggregate-size.sh: New test.
+       * testfile-sizes1.o.bz2: New test file.
+       * testfile-sizes2.o.bz2: Likewise.
+
 2014-09-26  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
        Support NT_FILE for locating files.
index ebf4f71f4fe9aec051d7038477ae1c74ba073e83..1c4b2b19202174bfece28325059023ef2d64b1ec 100644 (file)
@@ -50,7 +50,7 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
                  test-elf_cntl_gelf_getshdr dwflsyms dwfllines \
                  dwfl-report-elf-align varlocs backtrace backtrace-child \
                  backtrace-data backtrace-dwarf debuglink debugaltlink \
-                 buildid deleted deleted-lib.so
+                 buildid deleted deleted-lib.so aggregate_size
 
 asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
            asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -110,7 +110,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
        run-backtrace-core-aarch64.sh \
        run-backtrace-demangle.sh run-stack-d-test.sh run-stack-i-test.sh \
        run-readelf-dwz-multi.sh run-allfcts-multi.sh run-deleted.sh \
-       run-linkmap-cut.sh
+       run-linkmap-cut.sh run-aggregate-size.sh
 
 if !BIARCH
 export ELFUTILS_DISABLE_BIARCH = 1
@@ -274,7 +274,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
             testfiledwarfinlines.bz2 testfiledwarfinlines.core.bz2 \
             run-readelf-zdebug.sh testfile-debug.bz2 testfile-zdebug.bz2 \
             run-deleted.sh run-linkmap-cut.sh linkmap-cut-lib.so.bz2 \
-            linkmap-cut.bz2 linkmap-cut.core.bz2 
+            linkmap-cut.bz2 linkmap-cut.core.bz2 \
+            run-aggregate-size.sh testfile-sizes1.o.bz2 testfile-sizes2.o.bz2
 
 if USE_VALGRIND
 valgrind_cmd='valgrind -q --error-exitcode=1 --run-libc-freeres=no'
@@ -412,6 +413,7 @@ buildid_LDADD = $(libdw) $(libelf)
 deleted_LDADD = ./deleted-lib.so
 deleted_lib_so_LDFLAGS = -shared -rdynamic
 deleted_lib_so_CFLAGS = -fPIC
+aggregate_size_LDADD = $(libdw) $(libelf)
 
 if GCOV
 check: check-am coverage
diff --git a/tests/aggregate_size.c b/tests/aggregate_size.c
new file mode 100644 (file)
index 0000000..930eafa
--- /dev/null
@@ -0,0 +1,83 @@
+/* Test program for dwarf_aggregate_size. Prints size of top-level vars.
+   Copyright (C) 2014 Red Hat, Inc.
+   This file is part of elfutils.
+
+   This file 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.
+
+   elfutils 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/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <argp.h>
+#include <inttypes.h>
+#include <fcntl.h>
+#include ELFUTILS_HEADER(dw)
+#include ELFUTILS_HEADER(dwfl)
+#include <stdio.h>
+#include <unistd.h>
+#include <dwarf.h>
+
+void
+print_var_type_size (Dwarf_Die *var)
+{
+  Dwarf_Attribute attr_mem;
+  Dwarf_Die type_mem;
+  Dwarf_Die *type;
+  const char *name = dwarf_diename (var);
+
+  type = dwarf_formref_die (dwarf_attr (var, DW_AT_type, &attr_mem),
+                           &type_mem);
+  if (type != NULL)
+    {
+      Dwarf_Word size;
+      if (dwarf_aggregate_size (type, &size) < 0)
+        printf ("%s no size: %s\n", name, dwarf_errmsg (-1));
+      else
+       printf ("%s size %" PRIu64 "\n", name, size);
+    }
+  else
+    printf ("%s has no type.\n", name);
+}
+
+int
+main (int argc, char *argv[])
+{
+
+  int remaining;
+  Dwfl *dwfl;
+  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
+                     &dwfl);
+  assert (dwfl != NULL);
+
+  Dwarf_Die *cu = NULL;
+  Dwarf_Addr dwbias;
+  while ((cu = dwfl_nextcu (dwfl, cu, &dwbias)) != NULL)
+    {
+      Dwarf_Die die_mem;
+      Dwarf_Die *die = &die_mem;
+      dwarf_child (cu, &die_mem);
+
+      while (1)
+       {
+         if (dwarf_tag (die) == DW_TAG_variable)
+           print_var_type_size (die);
+
+         if (dwarf_siblingof (die, &die_mem) != 0)
+           break;
+       }
+    }
+
+  dwfl_end (dwfl);
+}
diff --git a/tests/run-aggregate-size.sh b/tests/run-aggregate-size.sh
new file mode 100755 (executable)
index 0000000..7d812a5
--- /dev/null
@@ -0,0 +1,67 @@
+#! /bin/sh
+# Copyright (C) 2014 Red Hat, Inc.
+# This file is part of elfutils.
+#
+# This file 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.
+#
+# elfutils 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/>.
+
+. $srcdir/test-subr.sh
+
+# char c;
+# int i;
+# long l;
+#
+# void *v;
+#
+# struct s
+# {
+#   char *a;
+#   int i;
+# } s;
+#
+# char ca[16];
+# int ia[32];
+# void *va[64];
+# struct s sa[8];
+
+# On x86_64 (LP64):
+# gcc -g -c -o testfile-sizes1.o sizes.c
+# clang -g -c -o testfile-sizes2.o sizes.c
+
+testfiles testfile-sizes1.o testfile-sizes2.o
+
+testrun_compare ${abs_builddir}/aggregate_size -e testfile-sizes1.o <<\EOF
+c size 1
+i size 4
+l size 8
+v size 8
+s size 16
+ca size 16
+ia size 128
+va size 512
+sa size 128
+EOF
+
+testrun_compare ${abs_builddir}/aggregate_size -e testfile-sizes2.o <<\EOF
+c size 1
+i size 4
+l size 8
+v size 8
+s size 16
+ca size 16
+ia size 128
+va size 512
+sa size 128
+EOF
+
+exit 0
diff --git a/tests/testfile-sizes1.o.bz2 b/tests/testfile-sizes1.o.bz2
new file mode 100644 (file)
index 0000000..479ecb2
Binary files /dev/null and b/tests/testfile-sizes1.o.bz2 differ
diff --git a/tests/testfile-sizes2.o.bz2 b/tests/testfile-sizes2.o.bz2
new file mode 100644 (file)
index 0000000..7bd7b47
Binary files /dev/null and b/tests/testfile-sizes2.o.bz2 differ