gdb: move the type cast into gdbarch_tdep
I built GDB for all targets on a x86-64/GNU-Linux system, and then (accidentally) passed GDB a RISC-V binary, and asked GDB to "run" the binary on the native target. I got this error: (gdb) show architecture The target architecture is set to "auto" (currently "i386"). (gdb) file /tmp/hello.rv32.exe Reading symbols from /tmp/hello.rv32.exe... (gdb) show architecture The target architecture is set to "auto" (currently "riscv:rv32"). (gdb) run Starting program: /tmp/hello.rv32.exe ../../src/gdb/i387-tdep.c:596: internal-error: i387_supply_fxsave: Assertion `tdep->st0_regnum >= I386_ST0_REGNUM' failed. What's going on here is this; initially the architecture is i386, this is based on the default architecture, which is set based on the native target. After loading the RISC-V executable the architecture of the current inferior is updated based on the architecture of the executable. When we "run", GDB does a fork & exec, with the inferior being controlled through ptrace. GDB sees an initial stop from the inferior as soon as the inferior comes to life. In response to this stop GDB ends up calling save_stop_reason (linux-nat.c), which ends up trying to read register from the inferior, to do this we end up calling target_ops::fetch_registers, which, for the x86-64 native target, calls amd64_linux_nat_target::fetch_registers. After this I eventually end up in i387_supply_fxsave, different x86 based targets will end in different functions to fetch registers, but it doesn't really matter which function we end up in, the problem is this line, which is repeated in many places: i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (arch); The problem here is that the ARCH in this line comes from the current inferior, which, as we discussed above, will be a RISC-V gdbarch, the tdep field will actually be of type riscv_gdbarch_tdep, not i386_gdbarch_tdep. After this cast we are relying on undefined behaviour, in my case I happen to trigger an assert, but this might not always be the case. The thing I tried that exposed this problem was of course, trying to start an executable of the wrong architecture on a native target. I don't think that the correct solution for this problem is to detect, at the point of cast, that the gdbarch_tdep object is of the wrong type, but, I did wonder, is there a way that we could protect ourselves from incorrectly casting the gdbarch_tdep object? I think that there is something we can do here, and this commit is the first step in that direction, though no actual check is added by this commit. This commit can be split into two parts: (1) In gdbarch.h and arch-utils.c. In these files I have modified gdbarch_tdep (the function) so that it now takes a template argument, like this: template<typename TDepType> static inline TDepType * gdbarch_tdep (struct gdbarch *gdbarch) { struct gdbarch_tdep *tdep = gdbarch_tdep_1 (gdbarch); return static_cast<TDepType *> (tdep); } After this change we are no better protected, but the cast is now done within the gdbarch_tdep function rather than at the call sites, this leads to the second, much larger change in this commit, (2) Everywhere gdbarch_tdep is called, we make changes like this: - i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (arch); + i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch); There should be no functional change after this commit. In the next commit I will build on this change to add an assertion in gdbarch_tdep that checks we are casting to the correct type.
This commit is contained in:
parent
602707187f
commit
08106042d9
118 changed files with 678 additions and 661 deletions
|
@ -142,7 +142,7 @@ struct m32c_gdbarch_tdep : gdbarch_tdep
|
|||
static void
|
||||
make_types (struct gdbarch *arch)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
|
||||
int data_addr_reg_bits, code_addr_reg_bits;
|
||||
char type_name[50];
|
||||
|
@ -215,7 +215,7 @@ make_types (struct gdbarch *arch)
|
|||
static const char *
|
||||
m32c_register_name (struct gdbarch *gdbarch, int num)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
return tdep->regs[num].name;
|
||||
}
|
||||
|
||||
|
@ -223,7 +223,7 @@ m32c_register_name (struct gdbarch *gdbarch, int num)
|
|||
static struct type *
|
||||
m32c_register_type (struct gdbarch *arch, int reg_nr)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
return tdep->regs[reg_nr].type;
|
||||
}
|
||||
|
||||
|
@ -231,7 +231,7 @@ m32c_register_type (struct gdbarch *arch, int reg_nr)
|
|||
static int
|
||||
m32c_register_sim_regno (struct gdbarch *gdbarch, int reg_nr)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
return tdep->regs[reg_nr].sim_num;
|
||||
}
|
||||
|
||||
|
@ -239,7 +239,7 @@ m32c_register_sim_regno (struct gdbarch *gdbarch, int reg_nr)
|
|||
static int
|
||||
m32c_debug_info_reg_to_regnum (struct gdbarch *gdbarch, int reg_nr)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
if (0 <= reg_nr && reg_nr <= M32C_MAX_DWARF_REGNUM
|
||||
&& tdep->dwarf_regs[reg_nr])
|
||||
return tdep->dwarf_regs[reg_nr]->num;
|
||||
|
@ -254,7 +254,7 @@ static int
|
|||
m32c_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
|
||||
const struct reggroup *group)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
struct m32c_reg *reg = &tdep->regs[regnum];
|
||||
|
||||
/* The anonymous raw registers aren't in any groups. */
|
||||
|
@ -330,7 +330,7 @@ static int
|
|||
m32c_read_flg (readable_regcache *cache)
|
||||
{
|
||||
gdbarch *arch = cache->arch ();
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
ULONGEST flg;
|
||||
|
||||
cache->raw_read (tdep->flg->num, &flg);
|
||||
|
@ -530,7 +530,7 @@ static enum register_status
|
|||
m32c_r3r2r1r0_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
|
||||
{
|
||||
gdbarch *arch = reg->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
int len = TYPE_LENGTH (tdep->r0->type);
|
||||
enum register_status status;
|
||||
|
||||
|
@ -567,7 +567,7 @@ m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache,
|
|||
const gdb_byte *buf)
|
||||
{
|
||||
gdbarch *arch = reg->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
int len = TYPE_LENGTH (tdep->r0->type);
|
||||
|
||||
if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
|
||||
|
@ -595,7 +595,7 @@ m32c_pseudo_register_read (struct gdbarch *arch,
|
|||
int cookednum,
|
||||
gdb_byte *buf)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
struct m32c_reg *reg;
|
||||
|
||||
gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
|
||||
|
@ -613,7 +613,7 @@ m32c_pseudo_register_write (struct gdbarch *arch,
|
|||
int cookednum,
|
||||
const gdb_byte *buf)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
struct m32c_reg *reg;
|
||||
|
||||
gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
|
||||
|
@ -638,7 +638,7 @@ add_reg (struct gdbarch *arch,
|
|||
struct m32c_reg *ry,
|
||||
int n)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
struct m32c_reg *r = &tdep->regs[tdep->num_regs];
|
||||
|
||||
gdb_assert (tdep->num_regs < M32C_MAX_NUM_REGS);
|
||||
|
@ -678,7 +678,7 @@ set_dwarf_regnum (struct m32c_reg *reg, int num)
|
|||
|
||||
/* Update the DWARF->reg mapping. */
|
||||
gdbarch *arch = reg->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
tdep->dwarf_regs[num] = reg;
|
||||
}
|
||||
|
||||
|
@ -800,7 +800,7 @@ mark_save_restore (struct m32c_reg *reg)
|
|||
static void
|
||||
make_regs (struct gdbarch *arch)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
int mach = gdbarch_bfd_arch_info (arch)->mach;
|
||||
int num_raw_regs;
|
||||
int num_cooked_regs;
|
||||
|
@ -1349,7 +1349,7 @@ m32c_pv_enter (struct m32c_pv_state *state, int size)
|
|||
return 1;
|
||||
|
||||
gdbarch *arch = state->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
if (m32c_pv_push (state, state->fb, tdep->push_addr_bytes))
|
||||
return 1;
|
||||
|
||||
|
@ -1379,7 +1379,7 @@ static int
|
|||
m32c_pv_pushm (struct m32c_pv_state *state, int src)
|
||||
{
|
||||
gdbarch *arch = state->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
/* The bits in SRC indicating which registers to save are:
|
||||
r0 r1 r2 r3 a0 a1 sb fb */
|
||||
|
@ -1400,7 +1400,7 @@ static int
|
|||
m32c_is_1st_arg_reg (struct m32c_pv_state *state, pv_t value)
|
||||
{
|
||||
gdbarch *arch = state->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
return (value.kind == pvk_register
|
||||
&& (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
|
||||
|
@ -1415,7 +1415,7 @@ static int
|
|||
m32c_is_arg_reg (struct m32c_pv_state *state, pv_t value)
|
||||
{
|
||||
gdbarch *arch = state->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
return (value.kind == pvk_register
|
||||
&& (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
|
||||
|
@ -1440,7 +1440,7 @@ m32c_is_arg_spill (struct m32c_pv_state *st,
|
|||
pv_t value)
|
||||
{
|
||||
gdbarch *arch = st->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
return (m32c_is_arg_reg (st, value)
|
||||
&& loc.kind == srcdest_mem
|
||||
|
@ -1464,7 +1464,7 @@ m32c_is_struct_return (struct m32c_pv_state *st,
|
|||
pv_t value)
|
||||
{
|
||||
gdbarch *arch = st->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
return (m32c_is_1st_arg_reg (st, value)
|
||||
&& !st->stack->find_reg (st->arch, value.reg, 0)
|
||||
|
@ -1482,7 +1482,7 @@ static int
|
|||
m32c_pushm_is_reg_save (struct m32c_pv_state *st, int src)
|
||||
{
|
||||
gdbarch *arch = st->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
/* The bits in SRC indicating which registers to save are:
|
||||
r0 r1 r2 r3 a0 a1 sb fb */
|
||||
|
@ -1510,7 +1510,7 @@ check_for_saved (void *prologue_untyped, pv_t addr, CORE_ADDR size, pv_t value)
|
|||
{
|
||||
struct m32c_prologue *prologue = (struct m32c_prologue *) prologue_untyped;
|
||||
struct gdbarch *arch = prologue->arch;
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
/* Is this the unchanged value of some register being saved on the
|
||||
stack? */
|
||||
|
@ -1550,7 +1550,7 @@ m32c_analyze_prologue (struct gdbarch *arch,
|
|||
CORE_ADDR start, CORE_ADDR limit,
|
||||
struct m32c_prologue *prologue)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
|
||||
CORE_ADDR after_last_frame_related_insn;
|
||||
struct m32c_pv_state st;
|
||||
|
@ -1881,7 +1881,7 @@ m32c_frame_base (struct frame_info *this_frame,
|
|||
struct m32c_prologue *p
|
||||
= m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
|
||||
gdbarch *arch = get_frame_arch (this_frame);
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
|
||||
/* In functions that use alloca, the distance between the stack
|
||||
pointer and the frame base varies dynamically, so we can't use
|
||||
|
@ -1932,7 +1932,7 @@ m32c_prev_register (struct frame_info *this_frame,
|
|||
void **this_prologue_cache, int regnum)
|
||||
{
|
||||
gdbarch *arch = get_frame_arch (this_frame);
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (arch);
|
||||
struct m32c_prologue *p
|
||||
= m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
|
||||
CORE_ADDR frame_base = m32c_frame_base (this_frame, this_prologue_cache);
|
||||
|
@ -2015,7 +2015,7 @@ m32c_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
|
|||
function_call_return_method return_method,
|
||||
CORE_ADDR struct_addr)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
|
||||
unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
|
||||
CORE_ADDR cfa;
|
||||
|
@ -2178,7 +2178,7 @@ m32c_return_value (struct gdbarch *gdbarch,
|
|||
gdb_byte *readbuf,
|
||||
const gdb_byte *writebuf)
|
||||
{
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
|
||||
enum return_value_convention conv;
|
||||
ULONGEST valtype_len = TYPE_LENGTH (valtype);
|
||||
|
@ -2309,7 +2309,7 @@ static CORE_ADDR
|
|||
m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
|
||||
{
|
||||
struct gdbarch *gdbarch = get_frame_arch (frame);
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
|
||||
|
||||
/* It would be nicer to simply look up the addresses of known
|
||||
|
@ -2555,7 +2555,7 @@ m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
|
|||
struct m32c_prologue p;
|
||||
|
||||
struct regcache *regcache = get_current_regcache ();
|
||||
m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
||||
m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
|
||||
|
||||
if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
|
||||
internal_error (__FILE__, __LINE__,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue