{ NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
{ "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
N_("Display DWARF section content. SECTION can be one of abbrev, "
- "aranges, frame, gdb_index, info, loc, line, ranges, pubnames, str, "
- "macinfo, macro or exception"), 0 },
+ "aranges, decodedaranges, frame, gdb_index, info, loc, line, ranges, "
+ "pubnames, str, macinfo, macro or exception"), 0 },
{ "hex-dump", 'x', "SECTION", 0,
N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
{ "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
/* True if we should print raw values instead of relativized addresses. */
static bool print_unresolved_addresses = false;
+/* True if we should print the .debug_aranges section using libdw. */
+static bool decodedaranges = false;
+
/* Select printing of debugging sections. */
static enum section_e
{
print_debug_sections |= section_abbrev;
else if (strcmp (arg, "aranges") == 0)
print_debug_sections |= section_aranges;
+ else if (strcmp (arg, "decodedaranges") == 0)
+ {
+ print_debug_sections |= section_aranges;
+ decodedaranges = true;
+ }
else if (strcmp (arg, "ranges") == 0)
{
print_debug_sections |= section_ranges;
not have to know a bit about the structure of the section, libdwarf
takes care of it. */
static void
-print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
- Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
- GElf_Shdr *shdr, Dwarf *dbg)
+print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
+ GElf_Shdr *shdr, Dwarf *dbg)
{
Dwarf_Aranges *aranges;
size_t cnt;
}
}
+
+/* Print content of DWARF .debug_aranges section. */
+static void
+print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
+ Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
+ GElf_Shdr *shdr, Dwarf *dbg)
+{
+ if (decodedaranges)
+ {
+ print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
+ return;
+ }
+
+ Elf_Data *data = elf_rawdata (scn, NULL);
+
+ if (unlikely (data == NULL))
+ {
+ error (0, 0, gettext ("cannot get .debug_aranges content: %s"),
+ elf_errmsg (-1));
+ return;
+ }
+
+ printf (gettext ("\
+\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
+ elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
+ (uint64_t) shdr->sh_offset);
+
+ const unsigned char *readp = data->d_buf;
+ const unsigned char *readendp = readp + data->d_size;
+
+ while (readp < readendp)
+ {
+ const unsigned char *hdrstart = readp;
+ size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
+
+ printf (gettext ("\nTable at offset %Zu:\n"), start_offset);
+ if (readp + 4 > readendp)
+ {
+ invalid_data:
+ error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
+ elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
+ return;
+ }
+
+ Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
+ unsigned int length_bytes = 4;
+ if (length == DWARF3_LENGTH_64_BIT)
+ {
+ if (readp + 8 > readendp)
+ goto invalid_data;
+ length = read_8ubyte_unaligned_inc (dbg, readp);
+ length_bytes = 8;
+ }
+
+ const unsigned char *nexthdr = readp + length;
+ printf (gettext ("\n Length: %6" PRIu64 "\n"),
+ (uint64_t) length);
+
+ if (nexthdr > readendp)
+ goto invalid_data;
+
+ if (length == 0)
+ continue;
+
+ if (readp + 2 > readendp)
+ goto invalid_data;
+ uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
+ printf (gettext (" DWARF version: %6" PRIuFAST16 "\n"),
+ version);
+ if (version != 2)
+ {
+ error (0, 0, gettext ("unsupported aranges version"));
+ goto next_table;
+ }
+
+ Dwarf_Word offset;
+ if (readp + length_bytes > readendp)
+ goto invalid_data;
+ if (length_bytes == 8)
+ offset = read_8ubyte_unaligned_inc (dbg, readp);
+ else
+ offset = read_4ubyte_unaligned_inc (dbg, readp);
+ printf (gettext (" CU offset: %6" PRIx64 "\n"),
+ (uint64_t) offset);
+
+ if (readp + 1 > readendp)
+ goto invalid_data;
+ unsigned int address_size = *readp++;
+ printf (gettext (" Address size: %6" PRIu64 "\n"),
+ (uint64_t) address_size);
+ if (address_size != 4 && address_size != 8)
+ {
+ error (0, 0, gettext ("unsupported address size"));
+ goto next_table;
+ }
+
+ unsigned int segment_size = *readp++;
+ printf (gettext (" Segment size: %6" PRIu64 "\n\n"),
+ (uint64_t) segment_size);
+ if (segment_size != 0 && segment_size != 4 && segment_size != 8)
+ {
+ error (0, 0, gettext ("unsupported segment size"));
+ goto next_table;
+ }
+
+ /* Round the address to the next multiple of 2*address_size. */
+ readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
+ % (2 * address_size));
+
+ while (readp < nexthdr)
+ {
+ Dwarf_Word range_address;
+ Dwarf_Word range_length;
+ Dwarf_Word segment = 0;
+ if (readp + 2 * address_size + segment_size > readendp)
+ goto invalid_data;
+ if (address_size == 4)
+ {
+ range_address = read_4ubyte_unaligned_inc (dbg, readp);
+ range_length = read_4ubyte_unaligned_inc (dbg, readp);
+ }
+ else
+ {
+ range_address = read_8ubyte_unaligned_inc (dbg, readp);
+ range_length = read_8ubyte_unaligned_inc (dbg, readp);
+ }
+
+ if (segment_size == 4)
+ segment = read_4ubyte_unaligned_inc (dbg, readp);
+ else if (segment_size == 8)
+ segment = read_8ubyte_unaligned_inc (dbg, readp);
+
+ if (range_address == 0 && range_length == 0 && segment == 0)
+ break;
+
+ char *b = format_dwarf_addr (dwflmod, address_size, range_address,
+ range_address);
+ char *e = format_dwarf_addr (dwflmod, address_size,
+ range_address + range_length - 1,
+ range_length);
+ if (segment_size != 0)
+ printf (gettext (" %s..%s (%" PRIx64 ")\n"), b, e,
+ (uint64_t) segment);
+ else
+ printf (gettext (" %s..%s\n"), b, e);
+ free (b);
+ free (e);
+ }
+
+ next_table:
+ if (readp != nexthdr)
+ {
+ size_t padding = nexthdr - readp;
+ printf (gettext (" %Zu padding bytes\n"), padding);
+ readp = nexthdr;
+ }
+ }
+}
+
+
/* Print content of DWARF .debug_ranges section. */
static void
print_debug_ranges_section (Dwfl_Module *dwflmod,
--- /dev/null
+#! /bin/sh
+# Copyright (C) 2013 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 readelf --debug-dump=aranges and --debug-dump=decodedaranges
+#
+# - foobarbaz.h
+#
+# int bar ();
+# int baz (int i);
+#
+# - bar.c
+#
+# #include "foobarbaz.h"
+#
+# static int bi;
+#
+# static int
+# barbaz (int i)
+# {
+# return i * 2 - 1;
+# }
+#
+# __attribute__ ((constructor)) void
+# nobar ()
+# {
+# bi = 1;
+# }
+#
+# int
+# bar ()
+# {
+# bi++;
+# return barbaz (bi);
+# }
+#
+# - foo.c
+#
+# include "foobarbaz.h"
+#
+# static int fi = 0;
+#
+# static int
+# foo (int i, int j)
+# {
+# if (i > j)
+# return i - j + fi;
+# else
+# return (2 * j) - i + fi;
+# }
+#
+# int
+# main (int argc, char **argv)
+# {
+# int a = bar ();
+# int b = baz (a + argc);
+# int r = foo (a, b) - 1;
+#
+# return r - 48;
+# }
+#
+# - baz.c
+# include "foobarbaz.h"
+#
+# static int bj;
+#
+# static int
+# bazbaz (int j)
+# {
+# return bj * j - bar ();
+# }
+#
+# __attribute__ ((constructor)) void
+# nobaz ()
+# {
+# bj = 1;
+# }
+#
+# int
+# baz (int i)
+# {
+# if (i < 0)
+# return bazbaz (i);
+# else
+# {
+# while (i-- > 0)
+# bj += bar ();
+# }
+# return bazbaz (i);
+# }
+#
+# gcc -g -O2 -m32 -c baz.c
+# gcc -g -O2 -m32 -c bar.c
+# gcc -g -O2 -m32 -c foo.c
+# gcc -g -O2 -m32 -o testfilefoobarbaz foo.o bar.o baz.o
+
+testfiles testfilefoobarbaz
+
+testrun_compare ../src/readelf --debug-dump=aranges testfilefoobarbaz <<EOF
+
+DWARF section [27] '.debug_aranges' at offset 0x1044:
+
+Table at offset 0:
+
+ Length: 28
+ DWARF version: 2
+ CU offset: 0
+ Address size: 4
+ Segment size: 0
+
+ 0x080482f0 <main>..0x08048323 <main+0x33>
+
+Table at offset 32:
+
+ Length: 36
+ DWARF version: 2
+ CU offset: 136
+ Address size: 4
+ Segment size: 0
+
+ 0x08048440 <bar>..0x08048451 <bar+0x11>
+ 0x08048330 <nobar>..0x0804833a <nobar+0xa>
+
+Table at offset 72:
+
+ Length: 36
+ DWARF version: 2
+ CU offset: 1d1
+ Address size: 4
+ Segment size: 0
+
+ 0x08048460 <baz>..0x080484bb <baz+0x5b>
+ 0x08048340 <nobaz>..0x0804834a <nobaz+0xa>
+EOF
+
+testrun_compare ../src/readelf --debug-dump=decodedaranges testfilefoobarbaz <<\EOF
+
+DWARF section [27] '.debug_aranges' at offset 0x1044 contains 5 entries:
+ [0] start: 0x080482f0, length: 52, CU DIE offset: 11
+ [1] start: 0x08048330, length: 11, CU DIE offset: 321
+ [2] start: 0x08048340, length: 11, CU DIE offset: 476
+ [3] start: 0x08048440, length: 18, CU DIE offset: 321
+ [4] start: 0x08048460, length: 92, CU DIE offset: 476
+EOF
+
+exit 0