From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 57AA1385E020; Mon, 4 Jul 2022 13:25:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57AA1385E020 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] alloc gas seginfo on notes obstack X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 0772daccb3ebaf513badf4266e1948454b4455c1 X-Git-Newrev: eeeaf705fe1c94e9330fa222d7928a9d0f03832a Message-Id: <20220704132510.57AA1385E020@sourceware.org> Date: Mon, 4 Jul 2022 13:25:10 +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, 04 Jul 2022 13:25:10 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Deeeaf705fe1c= 94e9330fa222d7928a9d0f03832a commit eeeaf705fe1c94e9330fa222d7928a9d0f03832a Author: Alan Modra Date: Mon Jul 4 12:45:47 2022 +0930 alloc gas seginfo on notes obstack =20 Lots of memory used in gas should go on this obstack. The patch also frees all the gas obstacks on exit, which isn't a completely trivial task. =20 * subsegs.c (alloc_seginfo): New function. (subseg_change, subseg_get): Use it. (subsegs_end): New function. * as.h (subsegs_end): Declare. * output-file.c: Include subsegs.h (stash_frchain_obs): New function. (output_file_close): Save obstacks attached to output bfd before closing. Call subsegs_end with the array of obstacks. Diff: --- gas/as.h | 1 + gas/output-file.c | 42 +++++++++++++++++++++++++++++++++++++----- gas/subsegs.c | 37 ++++++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/gas/as.h b/gas/as.h index 470a2e52891..ec0c12afe23 100644 --- a/gas/as.h +++ b/gas/as.h @@ -476,6 +476,7 @@ void input_scrub_end (void); void new_logical_line (const char *, int); void new_logical_line_flags (const char *, int, int); void subsegs_begin (void); +void subsegs_end (struct obstack **); void subseg_change (segT, int); segT subseg_new (const char *, subsegT); segT subseg_force_new (const char *, subsegT); diff --git a/gas/output-file.c b/gas/output-file.c index 9852a2ed456..95e21d23dc1 100644 --- a/gas/output-file.c +++ b/gas/output-file.c @@ -19,6 +19,7 @@ 02110-1301, USA. */ =20 #include "as.h" +#include "subsegs.h" #include "output-file.h" =20 #ifndef TARGET_MACH @@ -49,23 +50,54 @@ output_file_create (const char *name) stdoutput->flags |=3D BFD_TRADITIONAL_FORMAT; } =20 +static void +stash_frchain_obs (asection *sec) +{ + segment_info_type *info =3D seg_info (sec); + if (info) + { + struct frchain *frchp; + for (frchp =3D info->frchainP; frchp; frchp =3D frchp->frch_next) + obstack_ptr_grow (¬es, &frchp->frch_obstack); + info->frchainP =3D NULL; + } +} + void output_file_close (const char *filename) { bool res; + bfd *obfd =3D stdoutput; + struct obstack **obs; + asection *sec; =20 - if (stdoutput =3D=3D NULL) + if (obfd =3D=3D NULL) return; =20 + /* Prevent an infinite loop - if the close failed we will call as_fatal + which will call xexit() which may call this function again... */ + stdoutput =3D NULL; + + /* We can't free obstacks attached to the output bfd sections before + closing the output bfd since data in those obstacks may need to + be accessed, but we can't access anything in the output bfd after + it is closed.. */ + for (sec =3D obfd->sections; sec; sec =3D sec->next) + stash_frchain_obs (sec); + stash_frchain_obs (reg_section); + stash_frchain_obs (expr_section); + stash_frchain_obs (bfd_abs_section_ptr); + stash_frchain_obs (bfd_und_section_ptr); + obstack_ptr_grow (¬es, NULL); + obs =3D obstack_finish (¬es); + /* Close the bfd. */ if (!flag_always_generate_output && had_errors ()) res =3D bfd_cache_close_all (); else - res =3D bfd_close (stdoutput); + res =3D bfd_close (obfd); =20 - /* Prevent an infinite loop - if the close failed we will call as_fatal - which will call xexit() which may call this function again... */ - stdoutput =3D NULL; + subsegs_end (obs); =20 if (! res) as_fatal ("%s: %s", filename, bfd_errmsg (bfd_get_error ())); diff --git a/gas/subsegs.c b/gas/subsegs.c index cb598e84bef..1776511a9b8 100644 --- a/gas/subsegs.c +++ b/gas/subsegs.c @@ -43,7 +43,27 @@ subsegs_begin (void) frchain_now =3D NULL; /* Warn new_subseg() that we are booting. */ frag_now =3D &dummy_frag; } + +void +subsegs_end (struct obstack **obs) +{ + for (; *obs; obs++) + _obstack_free (*obs, NULL); + _obstack_free (&frchains, NULL); + _obstack_free (&cond_obstack, NULL); + _obstack_free (¬es, NULL); +} =0C +static void +alloc_seginfo (segT seg) +{ + segment_info_type *seginfo; + + seginfo =3D obstack_alloc (¬es, sizeof (*seginfo)); + memset (seginfo, 0, sizeof (*seginfo)); + seginfo->bfd_section =3D seg; + bfd_set_section_userdata (seg, seginfo); +} /* * subseg_change() * @@ -57,16 +77,11 @@ subsegs_begin (void) void subseg_change (segT seg, int subseg) { - segment_info_type *seginfo =3D seg_info (seg); now_seg =3D seg; now_subseg =3D subseg; =20 - if (! seginfo) - { - seginfo =3D XCNEW (segment_info_type); - seginfo->bfd_section =3D seg; - bfd_set_section_userdata (seg, seginfo); - } + if (!seg_info (seg)) + alloc_seginfo (seg); } =0C static void @@ -149,7 +164,6 @@ segT subseg_get (const char *segname, int force_new) { segT secptr; - segment_info_type *seginfo; const char *now_seg_name =3D now_seg ? bfd_section_name (now_seg) : 0; =20 if (!force_new @@ -163,13 +177,10 @@ subseg_get (const char *segname, int force_new) else secptr =3D bfd_make_section_anyway (stdoutput, segname); =20 - seginfo =3D seg_info (secptr); - if (! seginfo) + if (!seg_info (secptr)) { secptr->output_section =3D secptr; - seginfo =3D XCNEW (segment_info_type); - seginfo->bfd_section =3D secptr; - bfd_set_section_userdata (secptr, seginfo); + alloc_seginfo (secptr); } return secptr; }