From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 1062B385141E; Wed, 8 Mar 2023 03:17:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1062B385141E Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] Re: Move nm.c cached line number info to bfd usrdata X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: d64c8f7181fd21f90983f8d55369f6f9a2960c43 X-Git-Newrev: 160f20077865425a5874ed327540ab0bf33fb0d1 Message-Id: <20230308031743.1062B385141E@sourceware.org> Date: Wed, 8 Mar 2023 03:17:43 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Mar 2023 03:17:43 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D160f20077865= 425a5874ed327540ab0bf33fb0d1 commit 160f20077865425a5874ed327540ab0bf33fb0d1 Author: Alan Modra Date: Wed Mar 8 09:19:38 2023 +1030 Re: Move nm.c cached line number info to bfd usrdata =20 Commit e3f450f3933d resulted in a nm -l segfault on object files without undefined symbols. Fix that, and be paranoid about bfd section count changing. =20 * 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: --- binutils/nm.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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; }; =20 struct extended_symbol_info @@ -1165,9 +1166,8 @@ free_lineno_cache (bfd *abfd) =20 if (lc) { - unsigned int seccount =3D bfd_count_sections (abfd); - for (unsigned int i =3D 0; i < seccount; i++) - if (lc->relocs[i] !=3D NULL) + if (lc->relocs) + for (unsigned int i =3D 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 =3D bfd_count_sections (abfd); =20 /* For an undefined symbol, we try to find a reloc for the symbol, and print the line number of the reloc. */ if (lc->relocs =3D=3D NULL) { + unsigned int seccount =3D bfd_count_sections (abfd); + lc->seccount =3D seccount; lc->secs =3D xmalloc (seccount * sizeof (*lc->secs)); lc->relocs =3D xmalloc (seccount * sizeof (*lc->relocs)); lc->relcount =3D xmalloc (seccount * sizeof (*lc->relcount)); @@ -1269,7 +1270,7 @@ print_symbol (bfd * abfd, } =20 symname =3D bfd_asymbol_name (sym); - for (i =3D 0; i < seccount; i++) + for (i =3D 0; i < lc->seccount; i++) { long j; =20 @@ -1290,7 +1291,7 @@ print_symbol (bfd * abfd, { /* We only print the first one we find. */ printf ("\t%s:%u", filename, lineno); - i =3D seccount; + i =3D lc->seccount; break; } }