From: Mark Wielaard Date: Sun, 5 May 2019 21:18:36 +0000 (+0200) Subject: readelf: Decode DW_AT_discr_list block attributes. X-Git-Tag: elfutils-0.177~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=643cbb275d65533472c0f53391f9fc1d5d9a2efc;p=thirdparty%2Felfutils.git readelf: Decode DW_AT_discr_list block attributes. Decode DW_AT_descr_list blocks using the DW_DSC values. This requires knowing the signedness of the discriminant. Which means the attr_callback function needs access to the parent DIE. Pass the whole DIE path, plus the current level. That way the type of the discriminant can be looked up in the variant_part (parent) DIE of the variant DIE (which has the discr_list attribute). Add a testcase using both signed and unsigned discriminants. https://sourceware.org/bugzilla/show_bug.cgi?id=24509 Signed-off-by: Mark Wielaard --- diff --git a/src/ChangeLog b/src/ChangeLog index 0c817f417..580eea9b2 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2019-05-10 Mark Wielaard + + * readelf.c (struct attrcb_args): Rename die to dies. + (attr_callback): Get current current die using dies[level]. + Handle DW_AT_discr_list as block, not as constant. + (print_debug_units): pass dies, not dies[level] as args. + 2019-05-09 Mark Wielaard * readelf.c (cleanup_list): New function. diff --git a/src/readelf.c b/src/readelf.c index 4c20c38ec..062168bb9 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -6944,7 +6944,7 @@ struct attrcb_args { Dwfl_Module *dwflmod; Dwarf *dbg; - Dwarf_Die *die; + Dwarf_Die *dies; int level; bool silent; bool is_split; @@ -6960,7 +6960,7 @@ attr_callback (Dwarf_Attribute *attrp, void *arg) { struct attrcb_args *cbargs = (struct attrcb_args *) arg; const int level = cbargs->level; - Dwarf_Die *die = cbargs->die; + Dwarf_Die *die = &cbargs->dies[level]; bool is_split = cbargs->is_split; unsigned int attr = dwarf_whatattr (attrp); @@ -7304,9 +7304,6 @@ attr_callback (Dwarf_Attribute *attrp, void *arg) case DW_AT_ordering: valuestr = dwarf_ordering_name (num); break; - case DW_AT_discr_list: - valuestr = dwarf_discr_list_name (num); - break; case DW_AT_decl_file: case DW_AT_call_file: { @@ -7361,7 +7358,7 @@ attr_callback (Dwarf_Attribute *attrp, void *arg) /* When highpc is in constant form it is relative to lowpc. In that case also show the address. */ Dwarf_Addr highpc; - if (attr == DW_AT_high_pc && dwarf_highpc (cbargs->die, &highpc) == 0) + if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0) { printf (" %*s%-20s (%s) %" PRIuMAX " (", (int) (level * 2), "", dwarf_attr_name (attr), @@ -7383,7 +7380,7 @@ attr_callback (Dwarf_Attribute *attrp, void *arg) bool is_signed; int bytes = 0; if (attr == DW_AT_const_value) - die_type_sign_bytes (cbargs->die, &is_signed, &bytes); + die_type_sign_bytes (die, &is_signed, &bytes); else is_signed = (form == DW_FORM_sdata || form == DW_FORM_implicit_const); @@ -7538,6 +7535,96 @@ attr_callback (Dwarf_Attribute *attrp, void *arg) else print_block (block.length, block.data); break; + + case DW_AT_discr_list: + if (block.length == 0) + puts (""); + else if (form != DW_FORM_data16) + { + const unsigned char *readp = block.data; + const unsigned char *readendp = readp + block.length; + + /* See if we are dealing with a signed or unsigned + values. If the parent of this variant DIE is a + variant_part then it will either have a discriminant + which points to the member which type is the + discriminant type. Or the variant_part itself has a + type representing the discriminant. */ + bool is_signed = false; + if (level > 0) + { + Dwarf_Die *parent = &cbargs->dies[level - 1]; + if (dwarf_tag (die) == DW_TAG_variant + && dwarf_tag (parent) == DW_TAG_variant_part) + { + Dwarf_Die member; + Dwarf_Attribute discr_attr; + int bytes; + if (dwarf_formref_die (dwarf_attr (parent, + DW_AT_discr, + &discr_attr), + &member) != NULL) + die_type_sign_bytes (&member, &is_signed, &bytes); + else + die_type_sign_bytes (parent, &is_signed, &bytes); + } + } + while (readp < readendp) + { + int d = (int) *readp++; + printf ("%s ", dwarf_discr_list_name (d)); + if (readp >= readendp) + goto attrval_out; + + Dwarf_Word val; + Dwarf_Sword sval; + if (d == DW_DSC_label) + { + if (is_signed) + { + get_sleb128 (sval, readp, readendp); + printf ("%" PRId64 "", sval); + } + else + { + get_uleb128 (val, readp, readendp); + printf ("%" PRIu64 "", val); + } + } + else if (d == DW_DSC_range) + { + if (is_signed) + { + get_sleb128 (sval, readp, readendp); + printf ("%" PRId64 "..", sval); + if (readp >= readendp) + goto attrval_out; + get_sleb128 (sval, readp, readendp); + printf ("%" PRId64 "", sval); + } + else + { + get_uleb128 (val, readp, readendp); + printf ("%" PRIu64 "..", val); + if (readp >= readendp) + goto attrval_out; + get_uleb128 (val, readp, readendp); + printf ("%" PRIu64 "", val); + } + } + else + { + print_block (readendp - readp, readp); + break; + } + if (readp < readendp) + printf (", "); + } + putchar ('\n'); + } + else + print_block (block.length, block.data); + break; } break; @@ -7738,7 +7825,7 @@ print_debug_units (Dwfl_Module *dwflmod, /* Print the attribute values. */ args.level = level; - args.die = &dies[level]; + args.dies = dies; (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0); /* Make room for the next level's DIE. */ diff --git a/tests/ChangeLog b/tests/ChangeLog index 49392f1f2..15ac64f5d 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,12 @@ +2019-05-10 Mark Wielaard + + * Makefile.am (TESTS): Add run-readelf-discr.sh. + (EXTRA_DIST): Likewise and add testfile-rng.debug.bz2 and + testfile-urng.debug.bz2. + * run-readelf-discr.sh: New test. + * testfile-rng.debug.bz2: New test file. + * testfile-urng.debug.bz2: Likewise. + 2019-04-30 Mark Wielaard * xlate_notes.c: New file. diff --git a/tests/Makefile.am b/tests/Makefile.am index 498c1db26..80900e42d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -159,7 +159,8 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \ run-next-cfi.sh run-next-cfi-self.sh \ run-copyadd-sections.sh run-copymany-sections.sh \ run-typeiter-many.sh run-strip-test-many.sh \ - run-strip-version.sh run-xlate-note.sh + run-strip-version.sh run-xlate-note.sh \ + run-readelf-discr.sh if !BIARCH export ELFUTILS_DISABLE_BIARCH = 1 @@ -424,7 +425,9 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \ testfile-debug-rel-ppc64-z.o.bz2 \ testfile-debug-rel-ppc64.o.bz2 \ run-strip-version.sh testfile-version.bz2 \ - run-xlate-note.sh + run-xlate-note.sh \ + run-readelf-discr.sh \ + testfile-rng.debug.bz2 testfile-urng.debug.bz2 if USE_VALGRIND valgrind_cmd='valgrind -q --leak-check=full --error-exitcode=1' diff --git a/tests/run-readelf-discr.sh b/tests/run-readelf-discr.sh new file mode 100755 index 000000000..dc84d8272 --- /dev/null +++ b/tests/run-readelf-discr.sh @@ -0,0 +1,337 @@ +#! /bin/sh +# Copyright (C) 2019 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 . + +. $srcdir/test-subr.sh + +# = rng.ads = +# package Rng is +# +# type Rec (I : Integer) is record +# case I is +# when Positive => +# case I is +# when 1..15 | 17 | 23 => +# null; +# when others => +# J : Integer; +# end case; +# when -52..-1 => +# Q: Integer; +# when -64 => +# R: Boolean; +# when others => +# null; +# end case; +# end record; +# +# R : Rec (1); +# +# end Rng; + +# = urng.ads = +# +# package Urng is +# +# type Unsigned is mod 65536; +# type Rec (U : Unsigned) is record +# case U is +# when 17 | 23 | 32768..65535 => +# null; +# when 256 => +# B: Boolean; +# when others => +# I : Integer; +# end case; +# end record; +# +# R : Rec (1); +# +# end Urng; + +# gcc -c -g -fgnat-encodings=minimal -gstrict-dwarf rng.ads +# eu-strip -g -f rng.debug rng.o +# gcc -c -g -fgnat-encodings=minimal -gstrict-dwarf urng.ads +# eu-strip -g -f urng.debug urng.o + +testfiles testfile-rng.debug testfile-urng.debug + +testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=info testfile-rng.debug testfile-urng.debug <