[PATCH 15/24] gdb: make so_list::lm_info a unique_ptr

Simon Marchi
2023-10-10T20:45:12Z

Make it a unique_ptr, so it gets automatically deleted when the so_list
is deleted.

Change-Id: Ib62d60ae2a80656239860b80e4359121c93da13d
---
gdb/nto-tdep.c | 2 +-
gdb/solib-aix.c | 4 ++--
gdb/solib-darwin.c | 7 ++++---
gdb/solib-dsbt.c | 7 ++++---
gdb/solib-frv.c | 13 +++++++------
gdb/solib-rocm.c | 4 ++--
gdb/solib-svr4.c | 25 +++++++++++++++----------
gdb/solib-target.c | 4 ++--
gdb/solib.c | 1 -
gdb/solist.h | 4 +++-
10 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 67e3c7042754..bcc32fd857ed 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -247,7 +247,7 @@ nto_parse_redirection (char *pargv[], const char **pin, const char **pout,
static CORE_ADDR
lm_addr (const so_list &so)
{
- auto *li = gdb::checked_static_cast<const lm_info_svr4 *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<const lm_info_svr4 *> (so.lm_info.get ());

return li->l_addr;
}
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 9d232a03ae7b..a9931a60f336 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -316,7 +316,7 @@ solib_aix_relocate_section_addresses (so_list &so, target_section *sec)
struct bfd_section *bfd_sect = sec->the_bfd_section;
bfd *abfd = bfd_sect->owner;
const char *section_name = bfd_section_name (bfd_sect);
- auto *info = gdb::checked_static_cast<lm_info_aix *> (so.lm_info);
+ auto *info = gdb::checked_static_cast<lm_info_aix *> (so.lm_info.get ());

if (strcmp (section_name, ".text") == 0)
{
@@ -487,7 +487,7 @@ solib_aix_current_sos (void)
new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
memcpy (new_solib->so_name, new_solib->so_original_name,
SO_NAME_MAX_PATH_SIZE);
- new_solib->lm_info = new lm_info_aix (info);
+ new_solib->lm_info = gdb::make_unique<lm_info_aix> (info);

/* Add it to the list. */
if (!start)
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index c42082cfb368..e97c693052bc 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -275,14 +275,15 @@ darwin_current_sos (void)
/* Create and fill the new so_list element. */
so_list *newobj = new so_list;

- lm_info_darwin *li = new lm_info_darwin;
- newobj->lm_info = li;
+ auto li = gdb::make_unique<lm_info_darwin> ();

strncpy (newobj->so_name, file_path.get (), SO_NAME_MAX_PATH_SIZE - 1);
newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
strcpy (newobj->so_original_name, newobj->so_name);
li->lm_addr = load_addr;

+ newobj->lm_info = std::move (li);
+
if (head == NULL)
head = newobj;
else
@@ -614,7 +615,7 @@ darwin_clear_solib (program_space *pspace)
static void
darwin_relocate_section_addresses (so_list &so, target_section *sec)
{
- auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info.get ());

sec->addr += li->lm_addr;
sec->endaddr += li->lm_addr;
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index dca4080dee15..6f4de80782a9 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -595,8 +595,7 @@ dsbt_current_sos (void)
}

so_list *sop = new so_list;
- lm_info_dsbt *li = new lm_info_dsbt;
- sop->lm_info = li;
+ auto li = gdb::make_unique<lm_info_dsbt> ();
li->map = loadmap;
/* Fetch the name. */
addr = extract_unsigned_integer (lm_buf.l_name,
@@ -618,6 +617,8 @@ dsbt_current_sos (void)
strcpy (sop->so_original_name, sop->so_name);
}

+ sop->lm_info = std::move (li);
+
*sos_next_ptr = sop;
sos_next_ptr = &sop->next;
}
@@ -883,7 +884,7 @@ static void
dsbt_relocate_section_addresses (so_list &so, target_section *sec)
{
int seg;
- auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info.get ());
int_elf32_dsbt_loadmap *map = li->map;

for (seg = 0; seg < map->nsegs; seg++)
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 47a81b88efe7..5cbab1493128 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -378,8 +378,7 @@ frv_current_sos (void)
}

so_list *sop = new so_list;
- lm_info_frv *li = new lm_info_frv;
- sop->lm_info = li;
+ auto li = gdb::make_unique<lm_info_frv> ();
li->map = loadmap;
li->got_value = got_addr;
li->lm_addr = lm_addr;
@@ -402,6 +401,8 @@ frv_current_sos (void)
strcpy (sop->so_original_name, sop->so_name);
}

+ sop->lm_info = std::move (li);
+
*sos_next_ptr = sop;
sos_next_ptr = &sop->next;
}
@@ -819,7 +820,7 @@ static void
frv_relocate_section_addresses (so_list &so, target_section *sec)
{
int seg;
- auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
int_elf32_fdpic_loadmap *map = li->map;

for (seg = 0; seg < map->nsegs; seg++)
@@ -860,7 +861,7 @@ frv_fdpic_find_global_pointer (CORE_ADDR addr)
for (struct so_list *so : current_program_space->solibs ())
{
int seg;
- auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
int_elf32_fdpic_loadmap *map = li->map;

for (seg = 0; seg < map->nsegs; seg++)
@@ -916,7 +917,7 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
{
for (struct so_list *so : current_program_space->solibs ())
{
- auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());

addr = find_canonical_descriptor_in_load_object
(entry_point, got_value, name, so->abfd, li);
@@ -1068,7 +1069,7 @@ frv_fetch_objfile_link_map (struct objfile *objfile)
of shared libraries. */
for (struct so_list *so : current_program_space->solibs ())
{
- auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());

if (so->objfile == objfile)
return li->lm_addr;
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 5016c383a807..3bd5dec6d052 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -181,7 +181,7 @@ rocm_solib_relocate_section_addresses (so_list &so,
return;
}

- auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
sec->addr = sec->addr + li->l_addr;
sec->endaddr = sec->endaddr + li->l_addr;
}
@@ -213,7 +213,7 @@ so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
for (const rocm_so &so : sos)
{
so_list *newobj = new so_list;
- newobj->lm_info = new lm_info_svr4 (*so.lm_info);
+ newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);

strncpy (newobj->so_name, so.name.c_str (), sizeof (newobj->so_name));
newobj->so_name[sizeof (newobj->so_name) - 1] = '\0';
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 9266929d2895..6a54cd332085 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -188,8 +188,10 @@ svr4_same (const char *gdb_name, const char *inferior_name,
static int
svr4_same (const so_list &gdb, const so_list &inferior)
{
- auto *lmg = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info);
- auto *lmi = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info);
+ auto *lmg
+ = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info.get ());
+ auto *lmi
+ = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info.get ());

return svr4_same (gdb.so_original_name, inferior.so_original_name,
*lmg, *lmi);
@@ -239,7 +241,7 @@ has_lm_dynamic_from_link_map (void)
static CORE_ADDR
lm_addr_check (const so_list &so, bfd *abfd)
{
- auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());

if (!li->l_addr_p)
{
@@ -979,7 +981,7 @@ svr4_free_objfile_observer (struct objfile *objfile)
static void
svr4_clear_so (const so_list &so)
{
- auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());

if (li != NULL)
li->l_addr_p = 0;
@@ -1001,7 +1003,7 @@ so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
sizeof (newobj->so_name) - 1);
newobj->so_name[sizeof (newobj->so_name) - 1] = 0;
strcpy (newobj->so_original_name, newobj->so_name);
- newobj->lm_info = new lm_info_svr4 (*so.lm_info);
+ newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);

newobj->next = NULL;
*link = newobj;
@@ -1192,13 +1194,14 @@ svr4_default_sos (svr4_info *info)
return NULL;

so_list *newobj = new so_list;
- lm_info_svr4 *li = new lm_info_svr4;
- newobj->lm_info = li;
+ auto li = gdb::make_unique<lm_info_svr4> ();

/* Nothing will ever check the other fields if we set l_addr_p. */
li->l_addr = li->l_addr_inferior = info->debug_loader_offset;
li->l_addr_p = 1;

+ newobj->lm_info = std::move (li);
+
strncpy (newobj->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
strcpy (newobj->so_original_name, newobj->so_name);
@@ -1495,7 +1498,7 @@ svr4_current_sos (void)
[ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
*/

- auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info.get ());

if (address_in_mem_range (li->l_ld, &vsyscall_range))
{
@@ -1531,7 +1534,8 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
for (struct so_list *so : current_program_space->solibs ())
if (so->objfile == objfile)
{
- auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info);
+ auto *li
+ = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info.get ());

return li->lm_addr;
}
@@ -3292,7 +3296,8 @@ find_debug_base_for_solib (so_list *solib)
svr4_info *info = get_svr4_info (current_program_space);
gdb_assert (info != nullptr);

- const lm_info_svr4 *lm_info = (const lm_info_svr4 *) solib->lm_info;
+ auto *lm_info
+ = gdb::checked_static_cast<const lm_info_svr4 *> (solib->lm_info.get ());

for (const auto &tuple : info->solib_lists)
{
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index e20a336f9ad3..3743c77e9c76 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -260,7 +260,7 @@ solib_target_current_sos (void)
/* We no longer need this copy of the name. */
info->name.clear ();

- new_solib->lm_info = info.release ();
+ new_solib->lm_info = std::move (info);

/* Add it to the list. */
if (!start)
@@ -285,7 +285,7 @@ static void
solib_target_relocate_section_addresses (so_list &so, target_section *sec)
{
CORE_ADDR offset;
- auto *li = gdb::checked_static_cast<lm_info_target *> (so.lm_info);
+ auto *li = gdb::checked_static_cast<lm_info_target *> (so.lm_info.get ());

/* Build the offset table only once per object file. We can not do
it any earlier, since we need to open the file first. */
diff --git a/gdb/solib.c b/gdb/solib.c
index 4799fabe8213..5ed223b8dfca 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -675,7 +675,6 @@ void
free_so (so_list &so)
{
clear_so (so);
- delete so.lm_info;
delete &so;
}

diff --git a/gdb/solist.h b/gdb/solist.h
index 12ea516a10cf..5f9090a07c72 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -34,6 +34,8 @@ struct lm_info
virtual ~lm_info () = 0;
};

+using lm_info_up = std::unique_ptr<lm_info>;
+
struct so_list
{
/* The following fields of the structure come directly from the
@@ -46,7 +48,7 @@ struct so_list
will be a copy of struct link_map from the user process, but
it need not be; it can be any collection of data needed to
traverse the dynamic linker's data structures. */
- struct lm_info *lm_info = nullptr;
+ lm_info_up lm_info;

/* Shared object file name, exactly as it appears in the
inferior's link map. This may be a relative path, or something
--
2.42.0