This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 2/4] readelf: Add -z,--decompress option.


  -z, --decompress           Show compression information for compressed
                             sections (when used with -S).

Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
 src/ChangeLog          |   8 ++++
 src/readelf.c          |  66 +++++++++++++++++++++++++++--
 tests/ChangeLog        |   6 +++
 tests/Makefile.am      |   3 +-
 tests/run-readelf-z.sh | 110 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 189 insertions(+), 4 deletions(-)
 create mode 100755 tests/run-readelf-z.sh

diff --git a/src/ChangeLog b/src/ChangeLog
index d6d2936..4e62c8e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,13 @@
 2015-10-16  Mark Wielaard  <mjw@redhat.com>
 
+	* readelf.c (argp_option): Describe --decompress,-z.
+	(print_decompress): New bool.
+	(parse_opt): Handle -z.
+	(elf_ch_type_name): New function.
+	(print_shdr): Print section compress information.
+
+2015-10-16  Mark Wielaard  <mjw@redhat.com>
+
 	* Makefile.am [BUILD_STATIC](libdw): Add -lz.
 	[BUILD_STATIC](libelf): Likewise.
 
diff --git a/src/readelf.c b/src/readelf.c
index 5f6e4ed..1ab0946 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -48,6 +48,7 @@
 #include "../libelf/libelfP.h"
 #include "../libelf/common.h"
 #include "../libebl/libeblP.h"
+#include "../libdwelf/libdwelf.h"
 #include "../libdw/libdwP.h"
 #include "../libdwfl/libdwflP.h"
 #include "../libdw/memory-access.h"
@@ -112,6 +113,8 @@ static const struct argp_option options[] =
     N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
   { "wide", 'W', NULL, 0,
     N_("Ignored for compatibility (lines always wide)"), 0 },
+  { "decompress", 'z', NULL, 0,
+    N_("Show compression information for compressed sections (when used with -S)."), 0 },
   { NULL, 0, NULL, 0, NULL, 0 }
 };
 
@@ -190,6 +193,9 @@ static bool decodedaranges = false;
 /* True if we should print the .debug_aranges section using libdw.  */
 static bool decodedline = false;
 
+/* True if we want to show more information about compressed sections.  */
+static bool print_decompress = false;
+
 /* Select printing of debugging sections.  */
 static enum section_e
 {
@@ -479,6 +485,9 @@ parse_opt (int key, char *arg,
       break;
     case 'W':			/* Ignored.  */
       break;
+    case 'z':
+      print_decompress = true;
+      break;
     case ELF_INPUT_SECTION:
       if (arg == NULL)
 	elf_input_section = ".gnu_debugdata";
@@ -1065,6 +1074,17 @@ get_visibility_type (int value)
     }
 }
 
+static const char *
+elf_ch_type_name (unsigned int code)
+{
+  if (code == 0)
+    return "NONE";
+
+  if (code == ELFCOMPRESS_ZLIB)
+    return "ZLIB";
+
+  return "UNKNOWN";
+}
 
 /* Print the section headers.  */
 static void
@@ -1091,6 +1111,14 @@ There are %d section headers, starting at offset %#" PRIx64 ":\n\
   else
     puts (gettext ("[Nr] Name                 Type         Addr             Off      Size     ES Flags Lk Inf Al"));
 
+  if (print_decompress)
+    {
+      if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
+	puts (gettext ("     [Compression  Size   Al]"));
+      else
+	puts (gettext ("     [Compression  Size     Al]"));
+    }
+
   for (cnt = 0; cnt < shnum; ++cnt)
     {
       Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
@@ -1128,25 +1156,57 @@ There are %d section headers, starting at offset %#" PRIx64 ":\n\
 	*cp++ = 'G';
       if (shdr->sh_flags & SHF_TLS)
 	*cp++ = 'T';
+      if (shdr->sh_flags & SHF_COMPRESSED)
+	*cp++ = 'C';
       if (shdr->sh_flags & SHF_ORDERED)
 	*cp++ = 'O';
       if (shdr->sh_flags & SHF_EXCLUDE)
 	*cp++ = 'E';
       *cp = '\0';
 
+      const char *sname;
       char buf[128];
+      sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
       printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
 	      " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
 	      " %2" PRId64 "\n",
-	      cnt,
-	      elf_strptr (ebl->elf, shstrndx, shdr->sh_name)
-	      ?: "<corrupt>",
+	      cnt, sname,
 	      ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
 	      ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
 	      ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
 	      ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
 	      shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
 	      shdr->sh_addralign);
+
+      if (print_decompress)
+	{
+	  if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
+	    {
+	      DWElf_Chdr chdr;
+	      if (dwelf_scn_getchdr (scn, &chdr) != NULL)
+		printf ("     [ELF %s (%" PRId32 ") %0*" PRIx64
+			" %2" PRId64 "]\n",
+			elf_ch_type_name (chdr.ch_type),
+			chdr.ch_type,
+			ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
+			chdr.ch_size, chdr.ch_addralign);
+	      else
+		error (0, 0,
+		       gettext ("bad compression header for section %zd: %s"),
+		       elf_ndxscn (scn), elf_errmsg (-1));
+	    }
+	  else if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
+	    {
+	      ssize_t size;
+	      if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
+		printf ("     [GNU ZLIB     %0*" PRIx64 "   ]\n",
+			ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
+	      else
+		error (0, 0,
+		       gettext ("bad gnu compressed size for section %zd: %s"),
+		       elf_ndxscn (scn), elf_errmsg (-1));
+	    }
+	}
     }
 
   fputc_unlocked ('\n', stdout);
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 8a6f508..042e3a4 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,11 @@
 2015-10-28  Mark Wielaard  <mjw@redhat.com>
 
+	* run-readelf-z.sh: New test.
+	* Makefile.am (TESTS): Add run-readelf-z.sh.
+	(EXTRA_DIST): Likewise.
+
+2015-10-28  Mark Wielaard  <mjw@redhat.com>
+
 	* dwelfgetchdr.c: New file.
 	* run-dwelfgetchdr.sh: New test.
 	* testfile-zgnu32.bz2: New testfile.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a66b202..e9c9553 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -90,7 +90,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
 	run-readelf-test1.sh run-readelf-test2.sh run-readelf-test3.sh \
 	run-readelf-test4.sh run-readelf-twofiles.sh \
 	run-readelf-macro.sh run-readelf-loc.sh \
-	run-readelf-aranges.sh run-readelf-line.sh \
+	run-readelf-aranges.sh run-readelf-line.sh run-readelf-z.sh \
 	run-native-test.sh run-bug1-test.sh \
 	run-debuglink.sh run-debugaltlink.sh run-buildid.sh \
 	dwfl-bug-addr-overflow run-addrname-test.sh \
@@ -208,6 +208,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
 	     run-readelf-macro.sh testfilemacro.bz2 \
 	     run-readelf-loc.sh testfileloc.bz2 \
 	     run-readelf-aranges.sh run-readelf-line.sh testfilefoobarbaz.bz2 \
+	     run-readelf-z.sh \
 	     run-readelf-dwz-multi.sh libtestfile_multi_shared.so.bz2 \
 	     testfile_multi.dwz.bz2 testfile_multi_main.bz2 \
 	     testfile-dwzstr.bz2 testfile-dwzstr.multi.bz2 \
diff --git a/tests/run-readelf-z.sh b/tests/run-readelf-z.sh
new file mode 100755
index 0000000..4e4fcee
--- /dev/null
+++ b/tests/run-readelf-z.sh
@@ -0,0 +1,110 @@
+#! /bin/sh
+# Copyright (C) 2015 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
+
+# See run-elfgetchdr.sh for testfiles.
+
+testfiles testfile-zgnu64
+testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgnu64 <<\EOF
+There are 9 section headers, starting at offset 0x3e0:
+
+Section Headers:
+[Nr] Name                 Type         Addr             Off      Size     ES Flags Lk Inf Al
+     [Compression  Size     Al]
+[ 0]                      NULL         0000000000000000 00000000 00000000  0        0   0  0
+[ 1] .text                PROGBITS     0000000000400078 00000078 0000002a  0 AX     0   0  1
+[ 2] .zdebug_aranges      PROGBITS     0000000000000000 00000260 00000032  0        0   0 16
+     [GNU ZLIB     00000060   ]
+[ 3] .zdebug_info         PROGBITS     0000000000000000 00000292 0000006f  0        0   0  1
+     [GNU ZLIB     000000aa   ]
+[ 4] .debug_abbrev        PROGBITS     0000000000000000 00000301 00000028  0        0   0  1
+[ 5] .zdebug_line         PROGBITS     0000000000000000 00000329 0000005b  0        0   0  1
+     [GNU ZLIB     0000008d   ]
+[ 6] .shstrtab            STRTAB       0000000000000000 00000384 00000059  0        0   0  1
+[ 7] .symtab              SYMTAB       0000000000000000 000000a8 00000168 24        8   8  8
+[ 8] .strtab              STRTAB       0000000000000000 00000210 0000004b  0        0   0  1
+
+EOF
+
+testfiles testfile-zgabi64
+testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgabi64 <<\EOF
+There are 9 section headers, starting at offset 0x400:
+
+Section Headers:
+[Nr] Name                 Type         Addr             Off      Size     ES Flags Lk Inf Al
+     [Compression  Size     Al]
+[ 0]                      NULL         0000000000000000 00000000 00000000  0        0   0  0
+[ 1] .text                PROGBITS     0000000000400078 00000078 0000002a  0 AX     0   0  1
+[ 2] .debug_aranges       PROGBITS     0000000000000000 00000260 0000003e  0 C      0   0 16
+     [ELF ZLIB (1) 00000060 16]
+[ 3] .debug_info          PROGBITS     0000000000000000 0000029e 0000007b  0 C      0   0  1
+     [ELF ZLIB (1) 000000aa  1]
+[ 4] .debug_abbrev        PROGBITS     0000000000000000 00000319 00000028  0        0   0  1
+[ 5] .debug_line          PROGBITS     0000000000000000 00000341 00000067  0 C      0   0  1
+     [ELF ZLIB (1) 0000008d  1]
+[ 6] .shstrtab            STRTAB       0000000000000000 000003a8 00000056  0        0   0  1
+[ 7] .symtab              SYMTAB       0000000000000000 000000a8 00000168 24        8   8  8
+[ 8] .strtab              STRTAB       0000000000000000 00000210 0000004b  0        0   0  1
+
+EOF
+
+testfiles testfile-zgnu32
+testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgnu32 <<\EOF
+There are 9 section headers, starting at offset 0x33c:
+
+Section Headers:
+[Nr] Name                 Type         Addr     Off    Size   ES Flags Lk Inf Al
+     [Compression  Size   Al]
+[ 0]                      NULL         00000000 000000 000000  0        0   0  0
+[ 1] .text                PROGBITS     08048054 000054 00002a  0 AX     0   0  1
+[ 2] .zdebug_aranges      PROGBITS     00000000 0001c0 000031  0        0   0  8
+     [GNU ZLIB     000040   ]
+[ 3] .zdebug_info         PROGBITS     00000000 0001f1 00006f  0        0   0  1
+     [GNU ZLIB     00009a   ]
+[ 4] .debug_abbrev        PROGBITS     00000000 000260 000028  0        0   0  1
+[ 5] .zdebug_line         PROGBITS     00000000 000288 00005a  0        0   0  1
+     [GNU ZLIB     000085   ]
+[ 6] .shstrtab            STRTAB       00000000 0002e2 000059  0        0   0  1
+[ 7] .symtab              SYMTAB       00000000 000080 0000f0 16        8   8  4
+[ 8] .strtab              STRTAB       00000000 000170 00004b  0        0   0  1
+
+EOF
+
+testfiles testfile-zgabi32
+testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgabi32 <<\EOF
+There are 9 section headers, starting at offset 0x338:
+
+Section Headers:
+[Nr] Name                 Type         Addr     Off    Size   ES Flags Lk Inf Al
+     [Compression  Size   Al]
+[ 0]                      NULL         00000000 000000 000000  0        0   0  0
+[ 1] .text                PROGBITS     08048054 000054 00002a  0 AX     0   0  1
+[ 2] .debug_aranges       PROGBITS     00000000 0001c0 000031  0 C      0   0  8
+     [ELF ZLIB (1) 000040  8]
+[ 3] .debug_info          PROGBITS     00000000 0001f1 00006f  0 C      0   0  1
+     [ELF ZLIB (1) 00009a  1]
+[ 4] .debug_abbrev        PROGBITS     00000000 000260 000028  0        0   0  1
+[ 5] .debug_line          PROGBITS     00000000 000288 00005a  0 C      0   0  1
+     [ELF ZLIB (1) 000085  1]
+[ 6] .shstrtab            STRTAB       00000000 0002e2 000056  0        0   0  1
+[ 7] .symtab              SYMTAB       00000000 000080 0000f0 16        8   8  4
+[ 8] .strtab              STRTAB       00000000 000170 00004b  0        0   0  1
+
+EOF
+
+exit 0
-- 
2.4.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]