]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
readelf: Handle DW_OP_call2 and DW_OP_call4 correctly.
authorMark Wielaard <mark@klomp.org>
Thu, 7 Dec 2017 15:31:54 +0000 (16:31 +0100)
committerMark Wielaard <mark@klomp.org>
Mon, 11 Dec 2017 23:02:54 +0000 (00:02 +0100)
DW_OP_call2 and DW_OP_call4 didn't correctly advance the data pointer.
This caused print_ops to produce garbage operands. Also format the
arguments as DIE offsets. That makes it easier to follow the call to the
actual dwarf_procedure DIE.

Testcase from https://sourceware.org/bugzilla/show_bug.cgi?id=22532

The testcase only checks the eu-readelf output is correct for the
byte_size attribute. But it might be interesting to write a full
expression parser to check the actual sizes.

 [    3e]    structure_type
             name                 (strp) "pck__rec"
             byte_size            (exprloc)
              [   0] push_object_address
              [   1] deref_size 1
              [   3] call4 [    95]
              [   8] plus_uconst 7
              [  10] const1s -4
              [  12] and

 [    95]    dwarf_procedure
             location             (exprloc)
              [   0] dup
              [   1] lit1
              [   2] ne
              [   3] bra 10
              [   6] lit4
              [   7] skip 31
              [  10] dup
              [  11] lit4
              [  12] ne
              [  13] bra 20
              [  16] lit0
              [  17] skip 31
              [  20] dup
              [  21] lit3
              [  22] eq
              [  23] bra 30
              [  26] lit0
              [  27] skip 31
              [  30] lit4
              [  31] swap
              [  32] drop

The "answer" depends on the Discr value (first byte at object address),
and is rounded up to 4 or 8 bytes.

Signed-off-by: Mark Wielaard <mark@klomp.org>
src/ChangeLog
src/readelf.c
tests/ChangeLog
tests/Makefile.am
tests/run-readelf-variant.sh [new file with mode: 0755]
tests/testfile-ada-variant.bz2 [new file with mode: 0644]

index eed8569defa35d25fe8d1999e0f7744f76014c73..8935022bea09504d2cced5f4474ced17b5714e78 100644 (file)
@@ -1,3 +1,8 @@
+2017-12-07  Mark Wielaard  <mark@klomp.org>
+
+       * readelf.c (print_ops): Update data pointer and print arguments
+       to DW_OP_call2 and DW_OP_call4 as DIE offsets.
+
 2017-11-29  Mark Wielaard  <mark@klomp.org>
 
        * readelf.c (argp_options): Add "section-groups", 'g'.
index 357c73e3fd7bef2e221b3081684e3b1ece6c4cb0..ed21ef7acbe0a04aabb9479f9d7190207b00f188 100644 (file)
@@ -4324,19 +4324,21 @@ print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
 
        case DW_OP_call2:
          NEED (2);
-         printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
+         printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
                  indent, "", (uintmax_t) offset, op_name,
                  read_2ubyte_unaligned (dbg, data));
          CONSUME (2);
+         data += 2;
          offset += 3;
          break;
 
        case DW_OP_call4:
          NEED (4);
-         printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
+         printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
                  indent, "", (uintmax_t) offset, op_name,
                  read_4ubyte_unaligned (dbg, data));
          CONSUME (4);
+         data += 4;
          offset += 5;
          break;
 
index cb0c977fff223d374312919d837ed2534ed8999a..fe63359447fdac8c96e5964f376df933a3c71338 100644 (file)
@@ -1,3 +1,11 @@
+2017-12-07  Mark Wielaard  <mark@klomp.org>
+
+       * run-readelf-variant.sh: New test.
+       * testfile-ada-variant.bz2: New testfile.
+       * Makefile.am (TESTS): Add run-readelf-variant.sh.
+       (EXTRA_DISTS): Add run-readelf-variant.sh and
+       testfile-ada-variant.bz2.
+
 2017-11-29  Mark Wielaard  <mark@klomp.org>
 
        * run-readelf-loc.sh: Adjust expected loc list output.
index d5020547a3c0409814cc8b9eb352db7b896da94d..fca00727f46a813819b429ce22c3f4165ca9238d 100644 (file)
@@ -110,6 +110,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
        run-low_high_pc.sh run-macro-test.sh run-elf_cntl_gelf_getshdr.sh \
        run-test-archive64.sh run-readelf-vmcoreinfo.sh \
        run-readelf-mixed-corenote.sh run-dwfllines.sh \
+       run-readelf-variant.sh \
        run-dwfl-report-elf-align.sh run-addr2line-test.sh \
        run-addr2line-i-test.sh run-addr2line-i-lex-test.sh \
        run-addr2line-i-demangle-test.sh run-addr2line-alt-debugpath.sh \
@@ -264,6 +265,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
             testfilebazdynppc64.bz2 testfilebazmdbppc64.bz2 \
             testfilebazminppc64.bz2 testfilebazminppc64_pl.bz2 \
             testfilebazminppc64_plr.bz2 testfilebaztabppc64.bz2 \
+            run-readelf-variant.sh testfile-ada-variant.bz2 \
             run-dwflsyms.sh \
             run-unstrip-n.sh testcore-rtlib.bz2 testcore-rtlib-ppc.bz2 \
             run-low_high_pc.sh testfile_low_high_pc.bz2 \
diff --git a/tests/run-readelf-variant.sh b/tests/run-readelf-variant.sh
new file mode 100755 (executable)
index 0000000..dcc0d5e
--- /dev/null
@@ -0,0 +1,89 @@
+#! /bin/sh
+# Copyright (C) 2017 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
+
+# Tests exprloc for an Ada record variants byte_size.
+
+# = pck.ads
+#
+# with System;
+#
+# package Pck is
+#
+#    type One_To_Five is range 1 .. 5;
+#
+#    type Rec (Discr : One_To_Five) is
+#    record
+#       case Discr is
+#          when 1 => Field1 : Integer;
+#          when 4 => null;
+#          when 3 => Field3 : Boolean;
+#          when 5 => null;
+#          when others => null;
+#       end case;
+#    end record;
+#
+#    procedure Do_Nothing (A : System.Address);
+#
+# end Pck;
+
+# = pck.adb
+#
+# package body Pck is
+#
+#    procedure Do_Nothing (A : System.Address) is
+#    begin
+#       null;
+#    end Do_Nothing;
+#
+# end Pck;
+
+# = foo.adb
+#
+# with Pck; use Pck;
+#
+# procedure Foo is
+#
+#    R : Rec (1);
+#
+# begin
+#    Do_Nothing (R'Address);
+# end Foo;
+
+# gnatmake -g -fgnat-encodings=minimal foo.adb -cargs
+
+testfiles testfile-ada-variant
+
+tempfiles testfile.temp testfile2.temp
+
+testrun ${abs_top_builddir}/src/readelf --debug-dump=info \
+        testfile-ada-variant > testfile.temp
+
+grep -A6 byte_size testfile.temp | grep -A6 exprloc > testfile2.temp
+
+diff -u testfile2.temp - <<EOF
+             byte_size            (exprloc) 
+              [ 0] push_object_address
+              [ 1] deref_size 1
+              [ 3] call4 [    95]
+              [ 8] plus_uconst 7
+              [10] const1s -4
+              [12] and
+EOF
+
+exit 0
diff --git a/tests/testfile-ada-variant.bz2 b/tests/testfile-ada-variant.bz2
new file mode 100644 (file)
index 0000000..459774d
Binary files /dev/null and b/tests/testfile-ada-variant.bz2 differ