public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "Clément Chigot" <chigot@adacore.com>
To: binutils@sourceware.org
Cc: "Clément Chigot" <chigot@adacore.com>
Subject: [PATCH] binutils: improve holes detection in .debug_loclists.
Date: Thu,  1 Dec 2022 10:44:09 +0100	[thread overview]
Message-ID: <20221201094409.1982624-1-chigot@adacore.com> (raw)

The previous warnings about holes in .debug_loclists sections don't
take into account the headers of each CU and could include the locviews
if they precede the loclist.

The following warning can be triggered between two CU.
    ... <previous CU views> ...
    0000001d <End of list>

    0000002a v000000000000000 v000000000000000 location view pair
    0000002c v000000000000000 v000000000000000 location view pair

readelf: Warning: There is a hole [0x1e - 0x2e] in .debug_loclists section.
    0000002e v000000000000000 v000000000000000 views at 0000002a for:
    ...

But [0x1e - 0x2a] corresponds to the CU header and  [0x2a - 0x2e] are
the locviews.  Thus there is no hole here.

binutils/ChangeLog:

	* dwarf.c (display_debug_loc): Adjust holes detections for
	headers and locviews.
---
 binutils/dwarf.c | 47 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 14 deletions(-)

diff --git a/binutils/dwarf.c b/binutils/dwarf.c
index 404a0bdbac6..ffe3c10ff75 100644
--- a/binutils/dwarf.c
+++ b/binutils/dwarf.c
@@ -7158,7 +7158,7 @@ display_debug_loc (struct dwarf_section *section, void *file)
   const char *suffix = strrchr (section->name, '.');
   bool is_dwo = false;
   int is_loclists = strstr (section->name, "debug_loclists") != NULL;
-  uint64_t expected_start = 0;
+  uint64_t header_size = 0;
 
   if (suffix && strcmp (suffix, ".dwo") == 0)
     is_dwo = true;
@@ -7209,7 +7209,7 @@ display_debug_loc (struct dwarf_section *section, void *file)
       if (offset_entry_count != 0)
 	return display_offset_entry_loclists (section);
 
-      expected_start = hdrptr - section_begin;
+      header_size = hdrptr - section_begin;
     }
 
   if (load_debug_info (file) == 0)
@@ -7265,12 +7265,12 @@ display_debug_loc (struct dwarf_section *section, void *file)
     error (_("No location lists in .debug_info section!\n"));
 
   if (debug_information [first].num_loc_offsets > 0
-      && debug_information [first].loc_offsets [0] != expected_start
-      && debug_information [first].loc_views [0] != expected_start)
+      && debug_information [first].loc_offsets [0] != header_size
+      && debug_information [first].loc_views [0] != header_size)
     warn (_("Location lists in %s section start at %#" PRIx64
 	    " rather than %#" PRIx64 "\n"),
 	  section->name, debug_information [first].loc_offsets [0],
-	  expected_start);
+	  header_size);
 
   if (!locs_sorted)
     array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
@@ -7282,10 +7282,9 @@ display_debug_loc (struct dwarf_section *section, void *file)
 
   printf (_("    Offset   Begin            End              Expression\n"));
 
-  seen_first_offset = 0;
   for (i = first; i < num_debug_info_entries; i++)
     {
-      uint64_t offset, voffset;
+      uint64_t offset = 0, voffset = 0;
       uint64_t base_address;
       unsigned int k;
       int has_frame_base;
@@ -7300,6 +7299,24 @@ display_debug_loc (struct dwarf_section *section, void *file)
 		 sizeof (*array), loc_offsets_compar);
 	}
 
+      /* .debug_loclists has a per-unit header.
+	 Update start if we are detecting it.  */
+      if (debug_information [i].dwarf_version == 5)
+	{
+	  j = locs_sorted ? 0 : array [0];
+
+	  if (debug_information [i].num_loc_offsets)
+	    offset = debug_information [i].loc_offsets [j];
+
+	  if (debug_information [i].num_loc_views)
+	    voffset = debug_information [i].loc_views [j];
+
+	  /* Assume that the size of the header is constant across CUs. */
+	  if (((start - section_begin) + header_size == offset)
+	      || ((start -section_begin) + header_size == voffset))
+	    start += header_size;
+	}
+
       int adjacent_view_loclists = 1;
       for (k = 0; k < debug_information [i].num_loc_offsets; k++)
 	{
@@ -7330,19 +7347,21 @@ display_debug_loc (struct dwarf_section *section, void *file)
 		start = vstart;
 	    }
 
-	  if (!seen_first_offset || !adjacent_view_loclists)
-	    seen_first_offset = 1;
-	  else
+	  if (start < next)
 	    {
-	      if (start < next)
+	      if (vnext && vnext < next)
 		warn (_("There is a hole [%#tx - %#" PRIx64 "]"
 			" in %s section.\n"),
-		      start - section_begin, offset, section->name);
-	      else if (start > next)
-		warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
+		      start - section_begin, voffset, section->name);
+	      else
+		warn (_("There is a hole [%#tx - %#" PRIx64 "]"
 			" in %s section.\n"),
 		      start - section_begin, offset, section->name);
 	    }
+	  else if (start > next)
+	    warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
+		    " in %s section.\n"),
+		  start - section_begin, offset, section->name);
 	  start = next;
 	  vstart = vnext;
 
-- 
2.25.1


             reply	other threads:[~2022-12-01  9:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-01  9:44 Clément Chigot [this message]
2022-12-01 13:22 ` Nick Clifton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221201094409.1982624-1-chigot@adacore.com \
    --to=chigot@adacore.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).