From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 4BE533858414 for ; Mon, 6 Mar 2023 03:28:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4BE533858414 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-x531.google.com with SMTP id d6so4706482pgu.2 for ; Sun, 05 Mar 2023 19:28:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678073295; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=WHA7NwEAhk+ZacbpLxWgPEKQvAJJF0n8nbizuFnXWTY=; b=f+8uPxqIbzdjBNDuaXAkiaaVNldHcpIfI6/4Tin4rUcn+SVnCO0ZLNDhcl/Gue/uQP jo2X+OTlyz+STgzVd6HvKiE2AaMJwqVCBsaKx+HJRI8sTfnFDEeUC6EcWftB/6qfX++q O9/YY0AtkjeAi+lhRVBmCYhAPK53a9fhNCiEu0J3bj+EvDiAb4P4x3xKmGgZSy9XWL0q 65WuCddMIUf+5TvRpTntSjmDWFyCuVL4rRiqP9YMfCd921mjV3hk+S3IbAkdXNgHKBx3 tHHk7UVYle2KrzAxY66WYrXjd3lxSxOqH9OMLacLpUZeCpmlU1+U7kDg2Tk/9YOUAH3L pFOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678073295; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WHA7NwEAhk+ZacbpLxWgPEKQvAJJF0n8nbizuFnXWTY=; b=KF6TdM2rCWAJTVa2UH1B7r2t2T4tC8v0Zneu3Sc1sS9Z6ip3X66v97MWcYya0iGWZw 1Un2sx1dWD2nBIrNohJ9YbSrHReg95aDgBUyzk3JemLv9xr1ws8yjqG+74iiHrnIEeG9 kMs0I2c4hOo2r3HZyaA+CDxLxG7HwBH5YV1+fC2/yVN/1U3q2Nf7xRNAj+aY1L/NFhV7 zAl2mizBQGK31an9Os5zWhNa+Qdm9vLfnpluGps+4A6dHpx2VQ9qjiboY5jhQFJeyqag oKpA3pMmtRrS5AjuNblXl/cIzhJqbvm5eLknNH2xwIDCFl0cjTSosLxKst9Q8/Lj4uxc B11w== X-Gm-Message-State: AO0yUKXuAn95PfwSCQqfPhTmtKy0H1n6GfYBbIRCRR9rmgwKpob5dTl4 27QOou2ZSlFqXQCD42+a/VT9dpc2Efc= X-Google-Smtp-Source: AK7set/tzpIpXV5OcapLF4JeQ24427qgH59QR1Aksx5KQwvPw3hRY/doxfS7RCCLShTZEYUBqsLBsQ== X-Received: by 2002:aa7:9504:0:b0:5db:ba06:1825 with SMTP id b4-20020aa79504000000b005dbba061825mr9142479pfp.3.1678073295036; Sun, 05 Mar 2023 19:28:15 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:706e:b31e:8824:abae]) by smtp.gmail.com with ESMTPSA id d13-20020aa78e4d000000b005a8173829d5sm5226894pfr.66.2023.03.05.19.28.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 19:28:14 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 538011142C90; Mon, 6 Mar 2023 13:58:12 +1030 (ACDT) Date: Mon, 6 Mar 2023 13:58:12 +1030 From: Alan Modra To: binutils@sourceware.org Subject: Move nm.c cached line number info to bfd usrdata Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3034.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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. * 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 --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; }; -/* When fetching relocs, we use this structure to pass information to - get_relocs. */ +/* line number related info cached in bfd usrdata. */ -struct get_relocs_info +struct lineno_cache { asection **secs; arelent ***relocs; long *relcount; asymbol **syms; + long symcount; }; struct extended_symbol_info @@ -218,10 +218,6 @@ static const char *plugin_target = "plugin"; static const char *plugin_target = NULL; #endif -/* 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 = 0, @@ -1140,28 +1136,21 @@ sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms, static void get_relocs (bfd *abfd, asection *sec, void *dataarg) { - struct get_relocs_info *data = (struct get_relocs_info *) dataarg; + struct lineno_cache *data = (struct lineno_cache *) dataarg; *data->secs = sec; + *data->relocs = NULL; + *data->relcount = 0; - if ((sec->flags & SEC_RELOC) == 0) - { - *data->relocs = NULL; - *data->relcount = 0; - } - else + if ((sec->flags & SEC_RELOC) != 0) { - long relsize; - - relsize = bfd_get_reloc_upper_bound (abfd, sec); - if (relsize < 0) - bfd_fatal (bfd_get_filename (abfd)); - - *data->relocs = (arelent **) xmalloc (relsize); - *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs, - data->syms); - if (*data->relcount < 0) - bfd_fatal (bfd_get_filename (abfd)); + long relsize = bfd_get_reloc_upper_bound (abfd, sec); + if (relsize > 0) + { + *data->relocs = (arelent **) xmalloc (relsize); + *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs, + data->syms); + } } ++data->secs; @@ -1169,6 +1158,26 @@ get_relocs (bfd *abfd, asection *sec, void *dataarg) ++data->relcount; } +static void +free_lineno_cache (bfd *abfd) +{ + struct lineno_cache *lc = bfd_usrdata (abfd); + + if (lc) + { + unsigned int seccount = bfd_count_sections (abfd); + for (unsigned int i = 0; i < seccount; i++) + if (lc->relocs[i] != 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. */ static void @@ -1215,73 +1224,48 @@ print_symbol (bfd * abfd, if (line_numbers) { - static asymbol **syms; - static long symcount; + struct lineno_cache *lc = bfd_usrdata (abfd); const char *filename, *functionname; unsigned int lineno; /* 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 != lineno_cache_bfd && syms != NULL) + if (lc == NULL) { - free (syms); - syms = NULL; + lc = xcalloc (1, sizeof (*lc)); + bfd_set_usrdata (abfd, lc); } - if (syms == NULL) + if (lc->syms == NULL && lc->symcount == 0) { - long symsize; - - symsize = bfd_get_symtab_upper_bound (abfd); - if (symsize < 0) - bfd_fatal (bfd_get_filename (abfd)); - syms = (asymbol **) xmalloc (symsize); - symcount = bfd_canonicalize_symtab (abfd, syms); - if (symcount < 0) - bfd_fatal (bfd_get_filename (abfd)); - lineno_cache_bfd = abfd; + long symsize = bfd_get_symtab_upper_bound (abfd); + if (symsize <= 0) + lc->symcount = -1; + else + { + lc->syms = xmalloc (symsize); + lc->symcount = bfd_canonicalize_symtab (abfd, lc->syms); + } } - if (bfd_is_und_section (bfd_asymbol_section (sym))) + if (lc->symcount <= 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 = 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 (abfd != lineno_cache_rel_bfd && relocs != NULL) - { - for (i = 0; i < seccount; i++) - if (relocs[i] != NULL) - free (relocs[i]); - free (secs); - free (relocs); - free (relcount); - secs = NULL; - relocs = NULL; - relcount = NULL; - } - - if (relocs == NULL) + if (lc->relocs == NULL) { - struct get_relocs_info rinfo; + lc->secs = xmalloc (seccount * sizeof (*lc->secs)); + lc->relocs = xmalloc (seccount * sizeof (*lc->relocs)); + lc->relcount = xmalloc (seccount * sizeof (*lc->relcount)); - seccount = bfd_count_sections (abfd); - - secs = (asection **) xmalloc (seccount * sizeof *secs); - relocs = (arelent ***) xmalloc (seccount * sizeof *relocs); - relcount = (long *) xmalloc (seccount * sizeof *relcount); - - rinfo.secs = secs; - rinfo.relocs = relocs; - rinfo.relcount = relcount; - rinfo.syms = syms; - bfd_map_over_sections (abfd, get_relocs, (void *) &rinfo); - lineno_cache_rel_bfd = abfd; + struct lineno_cache rinfo = *lc; + bfd_map_over_sections (abfd, get_relocs, &rinfo); } symname = bfd_asymbol_name (sym); @@ -1289,17 +1273,17 @@ print_symbol (bfd * abfd, { long j; - for (j = 0; j < relcount[i]; j++) + for (j = 0; j < lc->relcount[i]; j++) { arelent *r; - r = relocs[i][j]; + r = lc->relocs[i][j]; if (r->sym_ptr_ptr != NULL && (*r->sym_ptr_ptr)->section == sym->section && (*r->sym_ptr_ptr)->value == sym->value && strcmp (symname, bfd_asymbol_name (*r->sym_ptr_ptr)) == 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 != NULL) @@ -1314,9 +1298,9 @@ print_symbol (bfd * abfd, } else if (bfd_asymbol_section (sym)->owner == 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 != NULL && lineno != 0) @@ -1624,9 +1608,8 @@ display_archive (bfd *file) if (last_arfile != NULL) { + free_lineno_cache (last_arfile); bfd_close (last_arfile); - lineno_cache_bfd = NULL; - lineno_cache_rel_bfd = NULL; if (arfile == last_arfile) return; } @@ -1635,9 +1618,8 @@ display_archive (bfd *file) if (last_arfile != NULL) { + free_lineno_cache (last_arfile); bfd_close (last_arfile); - lineno_cache_bfd = NULL; - lineno_cache_rel_bfd = NULL; } } @@ -1680,12 +1662,10 @@ display_file (char *filename) retval = false; } + free_lineno_cache (file); if (!bfd_close (file)) bfd_fatal (filename); - lineno_cache_bfd = NULL; - lineno_cache_rel_bfd = NULL; - return retval; } -- Alan Modra Australia Development Lab, IBM