[PATCH 11/24] gdb: use gdb::checked_static_cast when casting lm_info

Simon Marchi
2023-10-10T20:44:46Z

Now that the lm_info class hierarchy has a virtual destructor and
therefore a vtable, use checked_static_cast instead of C-style cases to
ensure (when building in dev mode) that we're casting to the right kind
of lm_info.

Change-Id: I9a99b7d6aa9a44edbe76377d57a7008cfb75a744
---
gdb/nto-tdep.c | 2 +-
gdb/solib-aix.c | 2 +-
gdb/solib-darwin.c | 2 +-
gdb/solib-dsbt.c | 2 +-
gdb/solib-frv.c | 8 ++++----
gdb/solib-rocm.c | 4 ++--
gdb/solib-svr4.c | 16 ++++++++--------
gdb/solib-target.c | 2 +-
8 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 1d9c7340fcf7..67e3c7042754 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)
{
- const lm_info_svr4 *li = (const lm_info_svr4 *) so.lm_info;
+ auto *li = gdb::checked_static_cast<const lm_info_svr4 *> (so.lm_info);

return li->l_addr;
}
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 7ec6462a8de1..9d232a03ae7b 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);
- lm_info_aix *info = (lm_info_aix *) so.lm_info;
+ auto *info = gdb::checked_static_cast<lm_info_aix *> (so.lm_info);

if (strcmp (section_name, ".text") == 0)
{
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index cb6ed3a70afc..c42082cfb368 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -614,7 +614,7 @@ darwin_clear_solib (program_space *pspace)
static void
darwin_relocate_section_addresses (so_list &so, target_section *sec)
{
- lm_info_darwin *li = (lm_info_darwin *) so.lm_info;
+ auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info);

sec->addr += li->lm_addr;
sec->endaddr += li->lm_addr;
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index af99afc33caa..dca4080dee15 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -883,7 +883,7 @@ static void
dsbt_relocate_section_addresses (so_list &so, target_section *sec)
{
int seg;
- lm_info_dsbt *li = (lm_info_dsbt *) so.lm_info;
+ auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info);
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 853d90a8c86c..47a81b88efe7 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -819,7 +819,7 @@ static void
frv_relocate_section_addresses (so_list &so, target_section *sec)
{
int seg;
- lm_info_frv *li = (lm_info_frv *) so.lm_info;
+ auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info);
int_elf32_fdpic_loadmap *map = li->map;

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

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

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

if (so->objfile == objfile)
return li->lm_addr;
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index e46d272b3339..403173ecc998 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -186,7 +186,7 @@ rocm_solib_relocate_section_addresses (so_list &so,
return;
}

- lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
+ auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
sec->addr = sec->addr + li->l_addr;
sec->endaddr = sec->endaddr + li->l_addr;
}
@@ -220,7 +220,7 @@ rocm_solib_copy_list (const so_list *src)
so_list *newobj = new so_list;
memcpy (newobj, src, sizeof (struct so_list));

- lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
+ auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
newobj->lm_info = new lm_info_svr4 (*src_li);

newobj->next = nullptr;
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 0bf8bb8446c7..7d247fa455e9 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -181,8 +181,8 @@ svr4_same (const so_list &gdb, const so_list &inferior)
/* There may be different instances of the same library, in different
namespaces. Each instance, however, must have been loaded at a
different address so its relocation offset would be different. */
- const lm_info_svr4 *lmg = (const lm_info_svr4 *) gdb.lm_info;
- const lm_info_svr4 *lmi = (const lm_info_svr4 *) inferior.lm_info;
+ 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);

return (lmg->l_addr_inferior == lmi->l_addr_inferior);
}
@@ -231,7 +231,7 @@ has_lm_dynamic_from_link_map (void)
static CORE_ADDR
lm_addr_check (const so_list &so, bfd *abfd)
{
- lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
+ auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);

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

if (li != NULL)
li->l_addr_p = 0;
@@ -1014,7 +1014,7 @@ svr4_copy_library_list (struct so_list *src)
so_list *newobj = new so_list;
memcpy (newobj, src, sizeof (struct so_list));

- lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
+ auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
newobj->lm_info = new lm_info_svr4 (*src_li);

newobj->next = NULL;
@@ -1561,7 +1561,7 @@ svr4_current_sos (void)
[ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
*/

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

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

return li->lm_addr;
}
@@ -1880,7 +1880,7 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
for (; solist->next != nullptr; solist = solist->next)
/* Nothing. */;

- lm_info_svr4 *li = (lm_info_svr4 *) solist->lm_info;
+ auto *li = gdb::checked_static_cast<lm_info_svr4 *> (solist->lm_info);
prev_lm = li->lm_addr;
pnext = &solist->next;
}
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index f79b59a36de0..05b35103091e 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -285,7 +285,7 @@ static void
solib_target_relocate_section_addresses (so_list &so, target_section *sec)
{
CORE_ADDR offset;
- lm_info_target *li = (lm_info_target *) so.lm_info;
+ auto *li = gdb::checked_static_cast<lm_info_target *> (so.lm_info);

/* Build the offset table only once per object file. We can not do
it any earlier, since we need to open the file first. */
--
2.42.0