From: Alan Modra Date: Fri, 31 Mar 2023 03:06:16 +0000 (+1030) Subject: rddbg.c stabs FIXMEs X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a6336913332;p=binutils-gdb.git rddbg.c stabs FIXMEs This should sort out some very old FIXMEs in code handling stabs debug info. Necessary if we are to fuss over freeing up memory before objdump and objcopy exit. It is of course better from a user viewpoint to *not* free memory, which takes some time, and leave that to process exit. The only reason to do so is that having many memory leaks in binutils/ code tends to hide leaks in bfd/ or opcodes/, which we should care about. * budbg.h (parse_stab): Update prototype. * debug.h (debug_start_source): Update prototype. * debug.c (debug_start_source): Add name_used. Set if stashed. * rddbg.c (read_symbol_stabs_debugging_info): Always malloc stab string passed to parse_stab. Free stab string when unreferenced. (read_section_stabs_debugging_info): Likewise, and strings section contents. * stabs.c (parse_stab): Add string_used param. Set if string stashed. Pass to debug_start_source. Realloc file_types array rather that using malloc. Clarify comment about debug_make_indirect_type. --- diff --git a/binutils/budbg.h b/binutils/budbg.h index a507ff9acf4..4a93b4e29b4 100644 --- a/binutils/budbg.h +++ b/binutils/budbg.h @@ -39,7 +39,7 @@ extern void *start_stab (void *, bfd *, bool, asymbol **, long); extern bool finish_stab (void *, void *); extern bool parse_stab - (void *, void *, int, int, bfd_vma, const char *); + (void *, void *, int, int, bfd_vma, const char *, bool *); extern bool write_stabs_in_sections_debugging_info (bfd *, void *, bfd_byte **, bfd_size_type *, bfd_byte **, bfd_size_type *); diff --git a/binutils/debug.c b/binutils/debug.c index ccd708f26d0..fd9a98ad01b 100644 --- a/binutils/debug.c +++ b/binutils/debug.c @@ -713,7 +713,7 @@ debug_set_filename (void *handle, const char *name) include files in a single compilation unit. */ bool -debug_start_source (void *handle, const char *name) +debug_start_source (void *handle, const char *name, bool *name_used) { struct debug_handle *info = (struct debug_handle *) handle; struct debug_file *f, **pf; @@ -740,6 +740,7 @@ debug_start_source (void *handle, const char *name) memset (f, 0, sizeof *f); f->filename = name; + *name_used = true; for (pf = &info->current_file->next; *pf != NULL; diff --git a/binutils/debug.h b/binutils/debug.h index 7dcb0878e66..fd19c8bc8d4 100644 --- a/binutils/debug.h +++ b/binutils/debug.h @@ -435,7 +435,7 @@ extern bool debug_set_filename (void *, const char *); /* Change source files to the given file name. This is used for include files in a single compilation unit. */ -extern bool debug_start_source (void *, const char *); +extern bool debug_start_source (void *, const char *, bool *); /* Record a function definition. This implicitly starts a function block. The debug_type argument is the type of the return value. diff --git a/binutils/rddbg.c b/binutils/rddbg.c index 15081c3f7c7..fb59f526979 100644 --- a/binutils/rddbg.c +++ b/binutils/rddbg.c @@ -194,6 +194,7 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, { size_t len; char *f, *s; + bool f_used; if (stroff + strx >= strsize) { @@ -238,10 +239,13 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, free (f); f = s; } + if (!f) + f = xstrdup (s); save_stab (type, desc, value, s); - if (! parse_stab (dhandle, shandle, type, desc, value, s)) + if (! parse_stab (dhandle, shandle, type, desc, value, + f, &f_used)) { stab_context (); free_saved_stabs (); @@ -252,18 +256,14 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, return false; } - /* Don't free f, since I think the stabs code - expects strings to hang around. This should be - straightened out. FIXME. */ + if (!f_used) + free (f); } } free_saved_stabs (); free (stabs); - - /* Don't free strings, since I think the stabs code expects - the strings to hang around. This should be straightened - out. FIXME. */ + free (strings); } } @@ -297,6 +297,7 @@ read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, { const char *s; char *f; + bool f_used; if (shandle == NULL) { @@ -327,20 +328,22 @@ read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, f = n; s = n; } + if (!f) + f = xstrdup (s); save_stab (i.stab_type, i.stab_desc, i.value, s); if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc, - i.value, s)) + i.value, f, &f_used)) { stab_context (); + free (f); free_saved_stabs (); return false; } - /* Don't free f, since I think the stabs code expects - strings to hang around. This should be straightened out. - FIXME. */ + if (!f_used) + free (f); } } diff --git a/binutils/stabs.c b/binutils/stabs.c index 85eebebafdd..f5a76dda5cc 100644 --- a/binutils/stabs.c +++ b/binutils/stabs.c @@ -422,11 +422,12 @@ finish_stab (void *dhandle, void *handle) bool parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value, - const char *string) + const char *string, bool *string_used) { const char * string_end; struct stab_handle *info = (struct stab_handle *) handle; + *string_used = false; /* gcc will emit two N_SO strings per compilation unit, one for the directory name and one for the file name. We just collect N_SO strings as we see them, and start the new compilation unit when @@ -447,11 +448,11 @@ parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value, info->file_start_offset = info->so_value; /* We need to reset the mapping from type numbers to types. We - can't free the old mapping, because of the use of - debug_make_indirect_type. */ + can only free the file_types array, not the stab_types + list entries due to the use of debug_make_indirect_type. */ info->files = 1; info->file_types = ((struct stab_types **) - xmalloc (sizeof *info->file_types)); + xrealloc (info->file_types, sizeof *info->file_types)); info->file_types[0] = NULL; info->so_string = NULL; @@ -566,21 +567,29 @@ parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value, case N_SOL: /* Start an include file. */ - if (! debug_start_source (dhandle, string)) + if (! debug_start_source (dhandle, string, string_used)) return false; break; case N_BINCL: /* Start an include file which may be replaced. */ + *string_used = true; push_bincl (info, string, value); - if (! debug_start_source (dhandle, string)) + if (! debug_start_source (dhandle, string, string_used)) return false; break; case N_EINCL: /* End an N_BINCL include. */ - if (! debug_start_source (dhandle, pop_bincl (info))) - return false; + string = pop_bincl (info); + if (! debug_start_source (dhandle, string, string_used)) + { + free ((char *) string); + return false; + } + if (!*string_used) + free ((char *) string); + *string_used = false; break; case N_EXCL: