From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 5689E3858401; Mon, 15 Aug 2022 14:55:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5689E3858401 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] PR29362, some binutils memory leaks X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 105afa7f230e5fed8f5a2eb28bc14d7696f5ac72 X-Git-Newrev: 450da4bd38ae529a6879baafe59b1e88507b5fd9 Message-Id: <20220815145527.5689E3858401@sourceware.org> Date: Mon, 15 Aug 2022 14:55:27 +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, 15 Aug 2022 14:55:27 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D450da4bd38ae= 529a6879baafe59b1e88507b5fd9 commit 450da4bd38ae529a6879baafe59b1e88507b5fd9 Author: Alan Modra Date: Tue Aug 16 00:16:49 2022 +0930 PR29362, some binutils memory leaks =20 2022-08-16 Alan Modra Cunlong Li =20 PR 29362 * dwarf.c (free_debug_information): New function, extracted.. (free_debug_memory): ..from here. (process_debug_info): Use it when before clearing out unit debug_information. Clear all fields. * objcopy.c (delete_symbol_htabs): New function. (main): Call it via xatexit. (copy_archive): Free "dir". * objdump.c (free_debug_section): Free reloc_info. Diff: --- binutils/dwarf.c | 52 ++++++++++++++++++++++++--------------------------= -- binutils/objcopy.c | 16 ++++++++++++++++ binutils/objdump.c | 3 +++ 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/binutils/dwarf.c b/binutils/dwarf.c index 09220978380..71d8bc9ed5c 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -3509,6 +3509,21 @@ introduce (struct dwarf_section * section, bool raw) } } =20 +/* Free memory allocated for one unit in debug_information. */ + +static void +free_debug_information (debug_info *ent) +{ + if (ent->max_loc_offsets) + { + free (ent->loc_offsets); + free (ent->loc_views); + free (ent->have_frame_base); + } + if (ent->max_range_lists) + free (ent->range_lists); +} + /* Process the contents of a .debug_info section. If do_loc is TRUE then we are scanning for location lists and dwo tags and we do not want to display anything to the user. @@ -3805,25 +3820,14 @@ process_debug_info (struct dwarf_section * section, && alloc_num_debug_info_entries > unit && ! do_types) { - debug_information [unit].cu_offset =3D cu_offset; - debug_information [unit].pointer_size - =3D compunit.cu_pointer_size; - debug_information [unit].offset_size =3D offset_size; - debug_information [unit].dwarf_version =3D compunit.cu_version; - debug_information [unit].base_address =3D 0; - debug_information [unit].addr_base =3D DEBUG_INFO_UNAVAILABLE; - debug_information [unit].ranges_base =3D DEBUG_INFO_UNAVAILABLE; - debug_information [unit].rnglists_base =3D 0; - debug_information [unit].loc_offsets =3D NULL; - debug_information [unit].have_frame_base =3D NULL; - debug_information [unit].max_loc_offsets =3D 0; - debug_information [unit].num_loc_offsets =3D 0; - debug_information [unit].loclists_base =3D 0; - debug_information [unit].range_lists =3D NULL; - debug_information [unit].max_range_lists=3D 0; - debug_information [unit].num_range_lists =3D 0; - debug_information [unit].rnglists_base =3D 0; - debug_information [unit].str_offsets_base =3D 0; + free_debug_information (&debug_information[unit]); + memset (&debug_information[unit], 0, sizeof (*debug_information)); + debug_information[unit].pointer_size =3D compunit.cu_pointer_size; + debug_information[unit].offset_size =3D offset_size; + debug_information[unit].dwarf_version =3D compunit.cu_version; + debug_information[unit].cu_offset =3D cu_offset; + debug_information[unit].addr_base =3D DEBUG_INFO_UNAVAILABLE; + debug_information[unit].ranges_base =3D DEBUG_INFO_UNAVAILABLE; } =20 if (!do_loc && dwarf_start_die =3D=3D 0) @@ -12021,15 +12025,7 @@ free_debug_memory (void) if (debug_information !=3D NULL) { for (i =3D 0; i < alloc_num_debug_info_entries; i++) - { - if (debug_information [i].max_loc_offsets) - { - free (debug_information [i].loc_offsets); - free (debug_information [i].have_frame_base); - } - if (debug_information [i].max_range_lists) - free (debug_information [i].range_lists); - } + free_debug_information (&debug_information[i]); free (debug_information); debug_information =3D NULL; alloc_num_debug_info_entries =3D num_debug_info_entries =3D 0; diff --git a/binutils/objcopy.c b/binutils/objcopy.c index b907b02d5e7..1118a2256a4 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -1048,6 +1048,20 @@ create_symbol_htabs (void) redefine_specific_reverse_htab =3D create_symbol_htab (); } =20 +static void +delete_symbol_htabs (void) +{ + htab_delete (strip_specific_htab); + htab_delete (strip_unneeded_htab); + htab_delete (keep_specific_htab); + htab_delete (localize_specific_htab); + htab_delete (globalize_specific_htab); + htab_delete (keepglobal_specific_htab); + htab_delete (weaken_specific_htab); + htab_delete (redefine_specific_htab); + htab_delete (redefine_specific_reverse_htab); +} + /* Add a symbol to strip_specific_list. */ =20 static void @@ -3736,6 +3750,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *outpu= t_target, } =20 rmdir (dir); + free (dir); } =20 static void @@ -6016,6 +6031,7 @@ main (int argc, char *argv[]) } =20 create_symbol_htabs (); + xatexit (delete_symbol_htabs); =20 if (argv !=3D NULL) bfd_set_error_program_name (argv[0]); diff --git a/binutils/objdump.c b/binutils/objdump.c index 188c286e294..b34df5392b9 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -4108,6 +4108,9 @@ free_debug_section (enum dwarf_section_display_enum d= ebug) section->start =3D NULL; section->address =3D 0; section->size =3D 0; + free ((char*) section->reloc_info); + section->reloc_info =3D NULL; + section->num_relocs=3D 0; } =20 void