public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: Re: Move nm.c cached line number info to bfd usrdata
Date: Wed, 8 Mar 2023 13:45:23 +1030	[thread overview]
Message-ID: <ZAf9y1US/95P0qWk@squeak.grove.modra.org> (raw)

Commit e3f450f3933d resulted in a nm -l segfault on object files
without undefined symbols.  Fix that, and be paranoid about bfd
section count changing.

	* nm.c (struct lineno_cache): Add seccount.
	(free_lineno_cache): Don't segfault on NULL lc->relocs.
	(print_symbol): Stash section count when creating arrays.

diff --git a/binutils/nm.c b/binutils/nm.c
index 8b6b249a951..f96cfa31cb9 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -62,6 +62,7 @@ struct lineno_cache
   long *relcount;
   asymbol **syms;
   long symcount;
+  unsigned int seccount;
 };
 
 struct extended_symbol_info
@@ -1165,9 +1166,8 @@ free_lineno_cache (bfd *abfd)
 
   if (lc)
     {
-      unsigned int seccount = bfd_count_sections (abfd);
-      for (unsigned int i = 0; i < seccount; i++)
-	if (lc->relocs[i] != NULL)
+      if (lc->relocs)
+	for (unsigned int i = 0; i < lc->seccount; i++)
 	  free (lc->relocs[i]);
       free (lc->relcount);
       free (lc->relocs);
@@ -1254,12 +1254,13 @@ print_symbol (bfd *        abfd,
 	{
 	  unsigned int i;
 	  const char *symname;
-	  unsigned int seccount = bfd_count_sections (abfd);
 
 	  /* For an undefined symbol, we try to find a reloc for the
              symbol, and print the line number of the reloc.  */
 	  if (lc->relocs == NULL)
 	    {
+	      unsigned int seccount = bfd_count_sections (abfd);
+	      lc->seccount = seccount;
 	      lc->secs = xmalloc (seccount * sizeof (*lc->secs));
 	      lc->relocs = xmalloc (seccount * sizeof (*lc->relocs));
 	      lc->relcount = xmalloc (seccount * sizeof (*lc->relcount));
@@ -1269,7 +1270,7 @@ print_symbol (bfd *        abfd,
 	    }
 
 	  symname = bfd_asymbol_name (sym);
-	  for (i = 0; i < seccount; i++)
+	  for (i = 0; i < lc->seccount; i++)
 	    {
 	      long j;
 
@@ -1290,7 +1291,7 @@ print_symbol (bfd *        abfd,
 		    {
 		      /* We only print the first one we find.  */
 		      printf ("\t%s:%u", filename, lineno);
-		      i = seccount;
+		      i = lc->seccount;
 		      break;
 		    }
 		}

-- 
Alan Modra
Australia Development Lab, IBM

             reply	other threads:[~2023-03-08  3:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-08  3:15 Alan Modra [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-03-06  3:28 Alan Modra

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=ZAf9y1US/95P0qWk@squeak.grove.modra.org \
    --to=amodra@gmail.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).