From 45fec14c029491329d769eae29021b4cb0cdd2c3 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 29 Mar 2023 22:05:59 +1030 Subject: Tidy leaked objcopy memory * objcopy.c (delete_symbol_htabs): Also free symbols. (write_debugging_info): Free strings and syms once written. * wrstabs.c (write_stabs_in_sections_debugging_info): memset entire info struct. Free hash tables before returning. Free syms on error return. --- binutils/objcopy.c | 13 ++++++++++--- binutils/wrstabs.c | 40 +++++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 22 deletions(-) (limited to 'binutils') diff --git a/binutils/objcopy.c b/binutils/objcopy.c index cf83044..72585ea 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -1065,6 +1065,10 @@ delete_symbol_htabs (void) htab_delete (weaken_specific_htab); htab_delete (redefine_specific_htab); htab_delete (redefine_specific_reverse_htab); + + free (isympp); + if (osympp != isympp) + free (osympp); } /* Add a symbol to strip_specific_list. */ @@ -4656,6 +4660,7 @@ write_debugging_info (bfd *obfd, void *dhandle, bfd_size_type symsize, stringsize; asection *stabsec, *stabstrsec; flagword flags; + bool ret; if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms, &symsize, &strings, @@ -4682,17 +4687,19 @@ write_debugging_info (bfd *obfd, void *dhandle, the next thing the caller is going to do is copy over the real sections. We may someday have to split the contents setting out of this function. */ + ret = true; if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize) || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0, stringsize)) { bfd_nonfatal_message (NULL, obfd, NULL, _("can't set debugging section contents")); - free (strings); - return false; + ret = false; } - return true; + free (strings); + free (syms); + return ret; } bfd_nonfatal_message (NULL, obfd, NULL, diff --git a/binutils/wrstabs.c b/binutils/wrstabs.c index 2df2b04..53ade2e 100644 --- a/binutils/wrstabs.c +++ b/binutils/wrstabs.c @@ -469,16 +469,18 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, struct string_hash_entry *h; bfd_byte *p; + memset (&info, 0, sizeof info); info.abfd = abfd; - info.symbols_size = 0; info.symbols_alloc = 500; info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc); - info.strings = NULL; - info.last_string = NULL; /* Reserve 1 byte for a null byte. */ info.strings_size = 1; + info.type_index = 1; + info.so_offset = -1; + info.fun_offset = -1; + info.pending_lbrac = (bfd_vma) -1; if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc, sizeof (struct string_hash_entry)) @@ -487,38 +489,28 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, { non_fatal ("bfd_hash_table_init_failed: %s", bfd_errmsg (bfd_get_error ())); - return false; + goto fail; } - info.type_stack = NULL; - info.type_index = 1; - memset (&info.type_cache, 0, sizeof info.type_cache); - info.so_offset = -1; - info.fun_offset = -1; - info.last_text_address = 0; - info.nesting = 0; - info.fnaddr = 0; - info.pending_lbrac = (bfd_vma) -1; - /* The initial symbol holds the string size. */ if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL)) - return false; + goto fail; /* Output an initial N_SO symbol. */ info.so_offset = info.symbols_size; if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd))) - return false; + goto fail; if (! debug_write (dhandle, &stab_fns, (void *) &info)) - return false; + goto fail; if (info.pending_lbrac != (bfd_vma) -1) - return false; + goto fail; /* Output a trailing N_SO. */ if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address, (const char *) NULL)) - return false; + goto fail; /* Put the string size in the initial symbol. */ bfd_put_32 (abfd, info.strings_size, info.symbols + 8); @@ -537,7 +529,17 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, p += strlen ((char *) p) + 1; } + bfd_hash_table_free (&info.typedef_hash.table); + bfd_hash_table_free (&info.strhash.table); return true; + + fail: + if (info.typedef_hash.table.memory) + bfd_hash_table_free (&info.typedef_hash.table); + if (info.strhash.table.memory) + bfd_hash_table_free (&info.strhash.table); + free (info.symbols); + return false; } /* Start writing out information for a compilation unit. */ -- cgit v1.1