[PATCH v2] dwarf: Properly check holes in .debug_ranges/debug_rnglists

H.J. Lu hjl.tools@gmail.com
Thu May 1 02:46:16 GMT 2025


On Thu, May 1, 2025 at 10:26 AM Alan Modra <amodra@gmail.com> wrote:
>
> On Thu, May 01, 2025 at 10:10:07AM +0930, Alan Modra wrote:
> > On Thu, May 01, 2025 at 04:58:50AM +0800, H.J. Lu wrote:
> > >     * testsuite/lib/binutils-common.exp (run_dump_test): Support
> > >     "prune_dump_output: REGEX" to prune dump output.
> >
> > "readelf: Warning: Range lists in .debug_rnglists section start at 0xc"
> > seems to me a silly warning.  That's exactly where you would expect to
> > find them, after the header!  The warning for ranges not starting at
> > offset zero is really only appropriate for .debug_ranges which doesn't
> > have a header.
> >
> > So instead of prune_dump_output, how hard would it be to get rid of
> > the readelf warning in the case where the range lists start exactly
> > where you would expect them to be?
>
> Like this, on top of your patch.  Replace the warning about where the
> range lists start into a warning about a hole.  Also fixes the hole
> warning.
>
> diff --git a/binutils/dwarf.c b/binutils/dwarf.c
> index 3c839197c10..5b3ece58c8b 100644
> --- a/binutils/dwarf.c
> +++ b/binutils/dwarf.c
> @@ -8424,10 +8424,6 @@ display_debug_ranges (struct dwarf_section *section,
>    qsort (range_entries, num_range_list, sizeof (*range_entries),
>          range_entry_compar);
>
> -  if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
> -    warn (_("Range lists in %s section start at %#" PRIx64 "\n"),
> -         section->name, range_entries[0].ranges_offset);
> -
>    putchar ('\n');
>    if (!is_rnglists)
>      printf (_("    Offset   Begin    End\n"));
> @@ -8471,6 +8467,12 @@ display_debug_ranges (struct dwarf_section *section,
>
>        next = section_begin + offset; /* Offset is from the section start, the base has already been added.  */
>
> +      if (i == 0)
> +       {
> +         last_end = section_begin;
> +         if (is_rnglists)
> +           last_end += 2 * offset_size - 4 + 2 + 1 + 1 + 4;
> +       }
>        /* If multiple DWARF entities reference the same range then we will
>          have multiple entries in the `range_entries' list for the same
>          offset.  Thanks to the sort above these will all be consecutive in
> @@ -8480,13 +8482,13 @@ display_debug_ranges (struct dwarf_section *section,
>         continue;
>        last_offset = offset;
>
> -      if (dwarf_check != 0 && i > 0)
> +      if (dwarf_check != 0)
>         {
>           if (start < next)
>             {
>               if (last_end != next)
>                 warn (_("There is a hole [%#tx - %#tx] in %s section.\n"),
> -                     start - section_begin, next - section_begin,
> +                     last_end - section_begin, next - section_begin,
>                       section->name);
>             }
>           else if (start > next)
> diff --git a/binutils/testsuite/binutils-all/x86-64/pr32927-2.d b/binutils/testsuite/binutils-all/x86-64/pr32927-2.d
> index 867c9fbf9b2..51ebe9370fd 100644
> --- a/binutils/testsuite/binutils-all/x86-64/pr32927-2.d
> +++ b/binutils/testsuite/binutils-all/x86-64/pr32927-2.d
> @@ -1,7 +1,6 @@
>  #source: dwarf5.s
>  #as: --64
>  #readelf: --dwarf-check --debug-dump=Ranges
> -#prune_dump_output: readelf: Warning: Range lists in \.debug_rnglists section start at 0xc
>
>  Contents of the \.debug_rnglists section:
>  #pass
> diff --git a/binutils/testsuite/lib/binutils-common.exp b/binutils/testsuite/lib/binutils-common.exp
> index 2bfd317320c..41d94d256ec 100644
> --- a/binutils/testsuite/lib/binutils-common.exp
> +++ b/binutils/testsuite/lib/binutils-common.exp
> @@ -938,10 +938,6 @@ proc prune_dump_output { output } {
>  #      Means the same as 'warning', except the regular expression
>  #      lines are contains in FILE.
>  #
> -#   prune_dump_output: REGEX
> -#       Messages which should be pruned from the output of the dump
> -#       program.
> -#
>  #   map: FILE
>  #      Adding this option will cause the linker to generate a linker
>  #      map file, using the -Map=MAPFILE command line option.  If
> @@ -1026,7 +1022,6 @@ proc run_dump_test { name {extra_options {}} } {
>      set opts(target) {}
>      set opts(warning) {}
>      set opts(warning_output) {}
> -    set opts(prune_dump_output) {}
>      set opts(xfail) {}
>      set opts(noxfail) {}
>
> @@ -1067,7 +1062,6 @@ proc run_dump_test { name {extra_options {}} } {
>             anyskip {}
>             noskip {}
>             warning {}
> -           prune_dump_output {}
>             error {}
>             source {
>                 # Move any source-specific as-flags to a separate list to
> @@ -1652,9 +1646,6 @@ proc run_dump_test { name {extra_options {}} } {
>         set cmdret [lindex $cmdret 0]
>         remote_upload host "dump.tmp"
>         set comp_output [prune_warnings [file_contents "dump.tmp"]]
> -       if { $opts(prune_dump_output) != "" } {
> -           regsub -all "(^|\n)$opts(prune_dump_output)\[^\n\]*\n?" $comp_output "\\1" comp_output
> -       }
>         remote_file host delete "dump.tmp"
>         remote_file build delete "dump.tmp"
>         if {[info exists old_lc_all]} {

Done.  Here is the v2 patch.

Don't warn if the offset of the first entry in .debug_rnglists starts
right after the header.  Warn holes in .debug_ranges and debug_rnglists
sections only if the last end pointer isn't the same as the current
start pointer.

PR binutils/32927
* dwarf.c (display_debug_ranges_list): Return the pointer to the
end.
(display_debug_ranges): Don't warn if the offset of the first
entry in .debug_rnglists starts right after the header.  Warn a
hole only if the last end pointer is the same as the next pointer.
* testsuite/binutils-all/x86-64/dwarf4.s: New file.
* testsuite/binutils-all/x86-64/dwarf5.s: Likewise.
* testsuite/binutils-all/x86-64/pr32927-1.d: Likewise.
* testsuite/binutils-all/x86-64/pr32927-2.d: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Co-Authored-By: Alan Modra <amodra@gmail.com>

Thanks.

-- 
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v2-0001-dwarf-Properly-check-holes-in-.debug_ranges-debug.patch.xz
Type: application/x-xz
Size: 89956 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20250501/d28bb1dc/attachment-0001.xz>


More information about the Binutils mailing list