Make the objfile destructor private

The idea behind this is that, in the long run, some code will need to
be able to hold onto an objfile after it is unlinked from the program
space.  In particular, this is needed for some functionality to be
moved to worker threads -- otherwise the objfile can be deleted while
still in use.

So, this makes ~objfile private, replacing it with an "unlink" method,
making it more obvious which operation is intended at the calling
points.

gdb/ChangeLog
2019-12-12  Tom Tromey  <tom@tromey.com>

	* symfile.c (syms_from_objfile_1): Use objfile_up.
	(syms_from_objfile_1, remove_symbol_file_command): Call unlink
	method.
	(reread_symbols): Use objfile_up.
	* solib.c (update_solib_list, reload_shared_libraries_1): Call
	unlink method.
	* objfiles.h (struct objfile) <~objfile>: Now private.
	<unlink>: New method.
	(struct objfile_deleter): New.
	(objfile_up): New typedef.
	* objfiles.c (objfile::unlink): New method.
	(free_objfile_separate_debug, free_all_objfiles)
	(objfile_purge_solibs): Use it.
	* jit.c (jit_unregister_code): Remove.
	(jit_inferior_exit_hook, jit_event_handler): Call unlink on
	objfile.
	* compile/compile-object-run.c (do_module_cleanup): Call unlink on
	objfile.
	* compile/compile-object-load.c (compile_object_load): Use
	objfile_up.

Change-Id: I934bee70b26b8b24e1735828fb1e60fe8a05714f
This commit is contained in:
Tom Tromey 2019-11-01 16:21:04 -06:00
parent f65fe5704a
commit 268e4f0914
8 changed files with 66 additions and 28 deletions

View File

@ -1,3 +1,26 @@
2019-12-12 Tom Tromey <tom@tromey.com>
* symfile.c (syms_from_objfile_1): Use objfile_up.
(syms_from_objfile_1, remove_symbol_file_command): Call unlink
method.
(reread_symbols): Use objfile_up.
* solib.c (update_solib_list, reload_shared_libraries_1): Call
unlink method.
* objfiles.h (struct objfile) <~objfile>: Now private.
<unlink>: New method.
(struct objfile_deleter): New.
(objfile_up): New typedef.
* objfiles.c (objfile::unlink): New method.
(free_objfile_separate_debug, free_all_objfiles)
(objfile_purge_solibs): Use it.
* jit.c (jit_unregister_code): Remove.
(jit_inferior_exit_hook, jit_event_handler): Call unlink on
objfile.
* compile/compile-object-run.c (do_module_cleanup): Call unlink on
objfile.
* compile/compile-object-load.c (compile_object_load): Use
objfile_up.
2019-12-12 Tom Tromey <tom@tromey.com>
* symfile.c (symbol_file_add_with_addrs): Pass "parent" to

View File

@ -632,9 +632,9 @@ compile_object_load (const compile_file_names &file_names,
/* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
"Reading symbols from ..." message for automatically generated file. */
std::unique_ptr<struct objfile> objfile_holder
(symbol_file_add_from_bfd (abfd.get (), filename.get (),
0, NULL, 0, NULL));
objfile_up objfile_holder (symbol_file_add_from_bfd (abfd.get (),
filename.get (),
0, NULL, 0, NULL));
objfile = objfile_holder.get ();
func_sym = lookup_global_symbol_from_objfile (objfile,

View File

@ -86,7 +86,7 @@ do_module_cleanup (void *arg, int registers_valid)
if ((objfile->flags & OBJF_USERLOADED) == 0
&& (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
{
delete objfile;
objfile->unlink ();
/* It may be a bit too pervasive in this dummy_frame dtor callback. */
clear_symtab_users (0);

View File

@ -951,18 +951,6 @@ jit_register_code (struct gdbarch *gdbarch,
jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch);
}
/* This function unregisters JITed code and frees the corresponding
objfile. */
static void
jit_unregister_code (struct objfile *objfile)
{
if (jit_debug)
fprintf_unfiltered (gdb_stdlog, "jit_unregister_code (%s)\n",
host_address_to_string (objfile));
delete objfile;
}
/* Look up the objfile with this code entry address. */
static struct objfile *
@ -1380,7 +1368,7 @@ jit_inferior_exit_hook (struct inferior *inf)
= (struct jit_objfile_data *) objfile_data (objf, jit_objfile_data);
if (objf_data != NULL && objf_data->addr != 0)
jit_unregister_code (objf);
objf->unlink ();
}
}
@ -1414,7 +1402,7 @@ jit_event_handler (struct gdbarch *gdbarch)
"entry at address: %s\n"),
paddress (gdbarch, entry_addr));
else
jit_unregister_code (objf);
objf->unlink ();
break;
default:

View File

@ -553,6 +553,14 @@ objfile::make (bfd *bfd_, const char *name_, objfile_flags flags_,
return result;
}
/* See objfiles.h. */
void
objfile::unlink ()
{
delete this;
}
/* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
itself. */
@ -564,7 +572,7 @@ free_objfile_separate_debug (struct objfile *objfile)
for (child = objfile->separate_debug_objfile; child;)
{
struct objfile *next_child = child->separate_debug_objfile_link;
delete child;
child->unlink ();
child = next_child;
}
}
@ -687,7 +695,7 @@ free_all_objfiles (void)
gdb_assert (so->objfile == NULL);
for (objfile *objfile : current_program_space->objfiles_safe ())
delete objfile;
objfile->unlink ();
clear_symtab_users (0);
}
@ -996,7 +1004,7 @@ objfile_purge_solibs (void)
be soon. */
if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
delete objf;
objf->unlink ();
}
}

View File

@ -399,13 +399,18 @@ private:
/* The only way to create an objfile is to call objfile::make. */
objfile (bfd *, const char *, objfile_flags);
/* The only way to free an objfile is via 'unlink'. */
~objfile ();
public:
/* Create an objfile. */
static objfile *make (bfd *bfd_, const char *name_, objfile_flags flags_,
objfile *parent = nullptr);
~objfile ();
/* Remove an objfile from the current program space, and free
it. */
void unlink ();
DISABLE_COPY_AND_ASSIGN (objfile);
@ -637,6 +642,20 @@ public:
htab_up static_links;
};
/* A deleter for objfile. */
struct objfile_deleter
{
void operator() (objfile *ptr) const
{
ptr->unlink ();
}
};
/* A unique pointer that holds an objfile. */
typedef std::unique_ptr<objfile, objfile_deleter> objfile_up;
/* Declarations for functions defined in objfiles.c */
extern struct gdbarch *get_objfile_arch (const struct objfile *);

View File

@ -835,7 +835,7 @@ update_solib_list (int from_tty)
/* Unless the user loaded it explicitly, free SO's objfile. */
if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
&& !solib_used (gdb))
delete gdb->objfile;
gdb->objfile->unlink ();
/* Some targets' section tables might be referring to
sections from so->abfd; remove them. */
@ -1312,7 +1312,7 @@ reload_shared_libraries_1 (int from_tty)
{
if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
&& !solib_used (so))
delete so->objfile;
so->objfile->unlink ();
remove_target_sections (so);
clear_so (so);
}

View File

@ -940,7 +940,7 @@ syms_from_objfile_1 (struct objfile *objfile,
if an error occurs during symbol reading. */
gdb::optional<clear_symtab_users_cleanup> defer_clear_users;
std::unique_ptr<struct objfile> objfile_holder (objfile);
objfile_up objfile_holder (objfile);
/* If ADDRS is NULL, put together a dummy address list.
We now establish the convention that an addr of zero means
@ -958,7 +958,7 @@ syms_from_objfile_1 (struct objfile *objfile,
if (symfile_objfile != NULL)
{
delete symfile_objfile;
symfile_objfile->unlink ();
gdb_assert (symfile_objfile == NULL);
}
@ -2438,7 +2438,7 @@ remove_symbol_file_command (const char *args, int from_tty)
objfile_name (objf)))
error (_("Not confirmed."));
delete objf;
objf->unlink ();
clear_symtab_users (0);
}
@ -2495,7 +2495,7 @@ reread_symbols (void)
/* If we get an error, blow away this objfile (not sure if
that is the correct response for things like shared
libraries). */
std::unique_ptr<struct objfile> objfile_holder (objfile);
objfile_up objfile_holder (objfile);
/* We need to do this whenever any symbols go away. */
clear_symtab_users_cleanup defer_clear_users (0);