From b7105b40ccd73a8e6b7fce6c11d2088eb1298b33 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Fri, 16 Oct 2015 22:21:23 +0200 Subject: readelf: Add -z,--decompress option. -z, --decompress Show compression information for compressed sections (when used with -S). Signed-off-by: Mark Wielaard --- src/ChangeLog | 8 ++ src/readelf.c | 66 +++++++++++++++- tests/ChangeLog | 6 ++ tests/Makefile.am | 3 +- tests/run-readelf-z.sh | 202 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 281 insertions(+), 4 deletions(-) create mode 100755 tests/run-readelf-z.sh diff --git a/src/ChangeLog b/src/ChangeLog index 0bbfa5b9..97242213 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2015-10-16 Mark Wielaard + + * 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-12-31 Mark Wielaard * elflint.c (check_symtab): Add _edata and _end (plus extra underscore diff --git a/src/readelf.c b/src/readelf.c index 5f6e4edd..1d507cf9 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) ?: ""; 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) - ?: "", + 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) + { + GElf_Chdr chdr; + if (gelf_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 7096f9f9..d9b92d3b 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,9 @@ +2015-10-28 Mark Wielaard + + * run-readelf-z.sh: New test. + * Makefile.am (TESTS): Add run-readelf-z.sh. + (EXTRA_DIST): Likewise. + 2015-10-28 Mark Wielaard * elfgetchdr.c: New file. diff --git a/tests/Makefile.am b/tests/Makefile.am index 2528e1c0..8ebb687b 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 \ @@ -209,6 +209,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 00000000..6dbd2f14 --- /dev/null +++ b/tests/run-readelf-z.sh @@ -0,0 +1,202 @@ +#! /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 . + +. $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-zgnu64be +testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgnu64be <<\EOF +There are 10 section headers, starting at offset 0x438: + +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 0000000010000078 00000078 00000074 0 AX 0 0 8 +[ 2] .eh_frame PROGBITS 00000000100000ec 000000ec 00000000 0 A 0 0 4 +[ 3] .zdebug_aranges PROGBITS 0000000000000000 000002c0 00000034 0 0 0 16 + [GNU ZLIB 00000060 ] +[ 4] .zdebug_info PROGBITS 0000000000000000 000002f4 00000059 0 0 0 1 + [GNU ZLIB 0000007e ] +[ 5] .debug_abbrev PROGBITS 0000000000000000 0000034d 00000028 0 0 0 1 +[ 6] .zdebug_line PROGBITS 0000000000000000 00000375 0000005b 0 0 0 1 + [GNU ZLIB 0000008d ] +[ 7] .shstrtab STRTAB 0000000000000000 000003d0 00000063 0 0 0 1 +[ 8] .symtab SYMTAB 0000000000000000 000000f0 00000180 24 9 9 8 +[ 9] .strtab STRTAB 0000000000000000 00000270 00000044 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-zgabi64be +testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgabi64be <<\EOF +There are 10 section headers, starting at offset 0x458: + +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 0000000010000078 00000078 00000074 0 AX 0 0 8 +[ 2] .eh_frame PROGBITS 00000000100000ec 000000ec 00000000 0 A 0 0 4 +[ 3] .debug_aranges PROGBITS 0000000000000000 000002c0 00000040 0 C 0 0 16 + [ELF ZLIB (1) 00000060 16] +[ 4] .debug_info PROGBITS 0000000000000000 00000300 00000065 0 C 0 0 1 + [ELF ZLIB (1) 0000007e 1] +[ 5] .debug_abbrev PROGBITS 0000000000000000 00000365 00000028 0 0 0 1 +[ 6] .debug_line PROGBITS 0000000000000000 0000038d 00000067 0 C 0 0 1 + [ELF ZLIB (1) 0000008d 1] +[ 7] .shstrtab STRTAB 0000000000000000 000003f4 00000060 0 0 0 1 +[ 8] .symtab SYMTAB 0000000000000000 000000f0 00000180 24 9 9 8 +[ 9] .strtab STRTAB 0000000000000000 00000270 00000044 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-zgnu32be +testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgnu32be <<\EOF +There are 10 section headers, starting at offset 0x390: + +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 01800054 000054 000074 0 AX 0 0 1 +[ 2] .eh_frame PROGBITS 018000c8 0000c8 000000 0 A 0 0 4 +[ 3] .zdebug_aranges PROGBITS 00000000 000220 000033 0 0 0 8 + [GNU ZLIB 000040 ] +[ 4] .zdebug_info PROGBITS 00000000 000253 000058 0 0 0 1 + [GNU ZLIB 00006e ] +[ 5] .debug_abbrev PROGBITS 00000000 0002ab 000028 0 0 0 1 +[ 6] .zdebug_line PROGBITS 00000000 0002d3 000059 0 0 0 1 + [GNU ZLIB 000085 ] +[ 7] .shstrtab STRTAB 00000000 00032c 000063 0 0 0 1 +[ 8] .symtab SYMTAB 00000000 0000c8 000110 16 9 9 4 +[ 9] .strtab STRTAB 00000000 0001d8 000045 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 + +testfiles testfile-zgabi32be +testrun_compare ${abs_top_builddir}/src/readelf -z -S testfile-zgabi32be <<\EOF +There are 10 section headers, starting at offset 0x38c: + +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 01800054 000054 000074 0 AX 0 0 1 +[ 2] .eh_frame PROGBITS 018000c8 0000c8 000000 0 A 0 0 4 +[ 3] .debug_aranges PROGBITS 00000000 000220 000033 0 C 0 0 8 + [ELF ZLIB (1) 000040 8] +[ 4] .debug_info PROGBITS 00000000 000253 000058 0 C 0 0 1 + [ELF ZLIB (1) 00006e 1] +[ 5] .debug_abbrev PROGBITS 00000000 0002ab 000028 0 0 0 1 +[ 6] .debug_line PROGBITS 00000000 0002d3 000059 0 C 0 0 1 + [ELF ZLIB (1) 000085 1] +[ 7] .shstrtab STRTAB 00000000 00032c 000060 0 0 0 1 +[ 8] .symtab SYMTAB 00000000 0000c8 000110 16 9 9 4 +[ 9] .strtab STRTAB 00000000 0001d8 000045 0 0 0 1 + +EOF + +exit 0 -- cgit v1.2.1