From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 50981385781A; Mon, 6 Mar 2023 03:33:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50981385781A 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] 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: a734d906cc0e0d3f32705e67b72e7113c69890d6 X-Git-Newrev: e3f450f3933ddb9ca4ef405722706f3c793d295e Message-Id: <20230306033313.50981385781A@sourceware.org> Date: Mon, 6 Mar 2023 03:33:13 +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: Mon, 06 Mar 2023 03:33:13 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3De3f450f3933d= db9ca4ef405722706f3c793d295e commit e3f450f3933ddb9ca4ef405722706f3c793d295e Author: Alan Modra Date: Mon Mar 6 10:42:36 2023 +1030 Move nm.c cached line number info to bfd usrdata =20 Replace the static variables used by nm to cache line number info with a struct attached to the bfd. Cleaner, and it avoids any concern that lineno_cache_bfd is somehow left pointing at memory for a closed bfd and that memory is later reused for another bfd, not that I think this is possible. Also don't bomb via bfd_fatal on errors getting the line number info, just omit the line numbers. =20 * nm.c (struct lineno_cache): Rename from get_relocs_info. Add symcount. (lineno_cache_bfd, lineno_cache_rel_bfd): Delete. (get_relocs): Adjust for struct rename. Don't call bfd_fatal on errors. (free_lineno_cache): New function. (print_symbol): Use lineno_cache in place of statics. Don't call bfd_fatal on errors reading symbols, just omit the line info. (display_archive, display_file): Call free_lineno_cache. Diff: --- binutils/nm.c | 150 +++++++++++++++++++++++++-----------------------------= ---- 1 file changed, 65 insertions(+), 85 deletions(-) diff --git a/binutils/nm.c b/binutils/nm.c index e91aa676931..2c428d20fdf 100644 --- a/binutils/nm.c +++ b/binutils/nm.c @@ -53,15 +53,15 @@ struct size_sym bfd_vma size; }; =20 -/* When fetching relocs, we use this structure to pass information to - get_relocs. */ +/* line number related info cached in bfd usrdata. */ =20 -struct get_relocs_info +struct lineno_cache { asection **secs; arelent ***relocs; long *relcount; asymbol **syms; + long symcount; }; =20 struct extended_symbol_info @@ -218,10 +218,6 @@ static const char *plugin_target =3D "plugin"; static const char *plugin_target =3D NULL; #endif =20 -/* Used to cache the line numbers for a BFD. */ -static bfd *lineno_cache_bfd; -static bfd *lineno_cache_rel_bfd; - typedef enum unicode_display_type { unicode_default =3D 0, @@ -1140,28 +1136,21 @@ sort_symbols_by_size (bfd *abfd, bool is_dynamic, v= oid *minisyms, static void get_relocs (bfd *abfd, asection *sec, void *dataarg) { - struct get_relocs_info *data =3D (struct get_relocs_info *) dataarg; + struct lineno_cache *data =3D (struct lineno_cache *) dataarg; =20 *data->secs =3D sec; + *data->relocs =3D NULL; + *data->relcount =3D 0; =20 - if ((sec->flags & SEC_RELOC) =3D=3D 0) - { - *data->relocs =3D NULL; - *data->relcount =3D 0; - } - else + if ((sec->flags & SEC_RELOC) !=3D 0) { - long relsize; - - relsize =3D bfd_get_reloc_upper_bound (abfd, sec); - if (relsize < 0) - bfd_fatal (bfd_get_filename (abfd)); - - *data->relocs =3D (arelent **) xmalloc (relsize); - *data->relcount =3D bfd_canonicalize_reloc (abfd, sec, *data->relocs, - data->syms); - if (*data->relcount < 0) - bfd_fatal (bfd_get_filename (abfd)); + long relsize =3D bfd_get_reloc_upper_bound (abfd, sec); + if (relsize > 0) + { + *data->relocs =3D (arelent **) xmalloc (relsize); + *data->relcount =3D bfd_canonicalize_reloc (abfd, sec, *data->relocs, + data->syms); + } } =20 ++data->secs; @@ -1169,6 +1158,26 @@ get_relocs (bfd *abfd, asection *sec, void *dataarg) ++data->relcount; } =20 +static void +free_lineno_cache (bfd *abfd) +{ + struct lineno_cache *lc =3D bfd_usrdata (abfd); + + 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) + free (lc->relocs[i]); + free (lc->relcount); + free (lc->relocs); + free (lc->secs); + free (lc->syms); + free (lc); + bfd_set_usrdata (abfd, NULL); + } +} + /* Print a single symbol. */ =20 static void @@ -1215,73 +1224,48 @@ print_symbol (bfd * abfd, =20 if (line_numbers) { - static asymbol **syms; - static long symcount; + struct lineno_cache *lc =3D bfd_usrdata (abfd); const char *filename, *functionname; unsigned int lineno; =20 /* We need to get the canonical symbols in order to call bfd_find_nearest_line. This is inefficient, but, then, you don't have to use --line-numbers. */ - if (abfd !=3D lineno_cache_bfd && syms !=3D NULL) + if (lc =3D=3D NULL) { - free (syms); - syms =3D NULL; + lc =3D xcalloc (1, sizeof (*lc)); + bfd_set_usrdata (abfd, lc); } - if (syms =3D=3D NULL) + if (lc->syms =3D=3D NULL && lc->symcount =3D=3D 0) { - long symsize; - - symsize =3D bfd_get_symtab_upper_bound (abfd); - if (symsize < 0) - bfd_fatal (bfd_get_filename (abfd)); - syms =3D (asymbol **) xmalloc (symsize); - symcount =3D bfd_canonicalize_symtab (abfd, syms); - if (symcount < 0) - bfd_fatal (bfd_get_filename (abfd)); - lineno_cache_bfd =3D abfd; + long symsize =3D bfd_get_symtab_upper_bound (abfd); + if (symsize <=3D 0) + lc->symcount =3D -1; + else + { + lc->syms =3D xmalloc (symsize); + lc->symcount =3D bfd_canonicalize_symtab (abfd, lc->syms); + } } =20 - if (bfd_is_und_section (bfd_asymbol_section (sym))) + if (lc->symcount <=3D 0) + ; + else if (bfd_is_und_section (bfd_asymbol_section (sym))) { - static asection **secs; - static arelent ***relocs; - static long *relcount; - static unsigned int seccount; 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 (abfd !=3D lineno_cache_rel_bfd && relocs !=3D NULL) - { - for (i =3D 0; i < seccount; i++) - if (relocs[i] !=3D NULL) - free (relocs[i]); - free (secs); - free (relocs); - free (relcount); - secs =3D NULL; - relocs =3D NULL; - relcount =3D NULL; - } - - if (relocs =3D=3D NULL) + if (lc->relocs =3D=3D NULL) { - struct get_relocs_info rinfo; + lc->secs =3D xmalloc (seccount * sizeof (*lc->secs)); + lc->relocs =3D xmalloc (seccount * sizeof (*lc->relocs)); + lc->relcount =3D xmalloc (seccount * sizeof (*lc->relcount)); =20 - seccount =3D bfd_count_sections (abfd); - - secs =3D (asection **) xmalloc (seccount * sizeof *secs); - relocs =3D (arelent ***) xmalloc (seccount * sizeof *relocs); - relcount =3D (long *) xmalloc (seccount * sizeof *relcount); - - rinfo.secs =3D secs; - rinfo.relocs =3D relocs; - rinfo.relcount =3D relcount; - rinfo.syms =3D syms; - bfd_map_over_sections (abfd, get_relocs, (void *) &rinfo); - lineno_cache_rel_bfd =3D abfd; + struct lineno_cache rinfo =3D *lc; + bfd_map_over_sections (abfd, get_relocs, &rinfo); } =20 symname =3D bfd_asymbol_name (sym); @@ -1289,17 +1273,17 @@ print_symbol (bfd * abfd, { long j; =20 - for (j =3D 0; j < relcount[i]; j++) + for (j =3D 0; j < lc->relcount[i]; j++) { arelent *r; =20 - r =3D relocs[i][j]; + r =3D lc->relocs[i][j]; if (r->sym_ptr_ptr !=3D NULL && (*r->sym_ptr_ptr)->section =3D=3D sym->section && (*r->sym_ptr_ptr)->value =3D=3D sym->value && strcmp (symname, bfd_asymbol_name (*r->sym_ptr_ptr)) =3D=3D 0 - && bfd_find_nearest_line (abfd, secs[i], syms, + && bfd_find_nearest_line (abfd, lc->secs[i], lc->syms, r->address, &filename, &functionname, &lineno) && filename !=3D NULL) @@ -1314,9 +1298,9 @@ print_symbol (bfd * abfd, } else if (bfd_asymbol_section (sym)->owner =3D=3D abfd) { - if ((bfd_find_line (abfd, syms, sym, &filename, &lineno) + if ((bfd_find_line (abfd, lc->syms, sym, &filename, &lineno) || bfd_find_nearest_line (abfd, bfd_asymbol_section (sym), - syms, sym->value, &filename, + lc->syms, sym->value, &filename, &functionname, &lineno)) && filename !=3D NULL && lineno !=3D 0) @@ -1624,9 +1608,8 @@ display_archive (bfd *file) =20 if (last_arfile !=3D NULL) { + free_lineno_cache (last_arfile); bfd_close (last_arfile); - lineno_cache_bfd =3D NULL; - lineno_cache_rel_bfd =3D NULL; if (arfile =3D=3D last_arfile) return; } @@ -1635,9 +1618,8 @@ display_archive (bfd *file) =20 if (last_arfile !=3D NULL) { + free_lineno_cache (last_arfile); bfd_close (last_arfile); - lineno_cache_bfd =3D NULL; - lineno_cache_rel_bfd =3D NULL; } } =20 @@ -1680,12 +1662,10 @@ display_file (char *filename) retval =3D false; } =20 + free_lineno_cache (file); if (!bfd_close (file)) bfd_fatal (filename); =20 - lineno_cache_bfd =3D NULL; - lineno_cache_rel_bfd =3D NULL; - return retval; }