mips-mdebug-tdep.c [plain text]
#include "defs.h"
#include "frame.h"
#include "mips-tdep.h"
#include "trad-frame.h"
#include "block.h"
#include "symtab.h"
#include "objfiles.h"
#include "elf/mips.h"
#include "elf-bfd.h"
#include "gdb_assert.h"
#include "frame-unwind.h"
#include "frame-base.h"
#include "mips-mdebug-tdep.h"
#include "mdebugread.h"
#define PROC_LOW_ADDR(proc) ((proc)->pdr.adr)
#define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
#define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
#define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
#define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
#define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
#define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
#define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
#define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
#define _PROC_MAGIC_ 0x0F0F0F0F
struct mips_objfile_private
{
bfd_size_type size;
char *contents;
};
static bfd *the_bfd;
static int
compare_pdr_entries (const void *a, const void *b)
{
CORE_ADDR lhs = bfd_get_32 (the_bfd, (bfd_byte *) a);
CORE_ADDR rhs = bfd_get_32 (the_bfd, (bfd_byte *) b);
if (lhs < rhs)
return -1;
else if (lhs == rhs)
return 0;
else
return 1;
}
static const struct objfile_data *mips_pdr_data;
static struct mdebug_extra_func_info *
non_heuristic_proc_desc (CORE_ADDR pc, CORE_ADDR *addrptr)
{
CORE_ADDR startaddr;
struct mdebug_extra_func_info *proc_desc;
struct block *b = block_for_pc (pc);
struct symbol *sym;
struct obj_section *sec;
struct mips_objfile_private *priv;
find_pc_partial_function (pc, NULL, &startaddr, NULL);
if (addrptr)
*addrptr = startaddr;
priv = NULL;
sec = find_pc_section (pc);
if (sec != NULL)
{
priv = (struct mips_objfile_private *) objfile_data (sec->objfile, mips_pdr_data);
the_bfd = sec->objfile->obfd;
if (priv == NULL
&& (the_bfd->format == bfd_object
&& bfd_get_flavour (the_bfd) == bfd_target_elf_flavour
&& elf_elfheader (the_bfd)->e_ident[EI_CLASS] == ELFCLASS64))
{
priv = obstack_alloc (&sec->objfile->objfile_obstack,
sizeof (struct mips_objfile_private));
priv->size = 0;
set_objfile_data (sec->objfile, mips_pdr_data, priv);
}
else if (priv == NULL)
{
asection *bfdsec;
priv = obstack_alloc (&sec->objfile->objfile_obstack,
sizeof (struct mips_objfile_private));
bfdsec = bfd_get_section_by_name (sec->objfile->obfd, ".pdr");
if (bfdsec != NULL)
{
priv->size = bfd_section_size (sec->objfile->obfd, bfdsec);
priv->contents = obstack_alloc (&sec->objfile->objfile_obstack,
priv->size);
bfd_get_section_contents (sec->objfile->obfd, bfdsec,
priv->contents, 0, priv->size);
qsort (priv->contents, priv->size / 32, 32,
compare_pdr_entries);
}
else
priv->size = 0;
set_objfile_data (sec->objfile, mips_pdr_data, priv);
}
the_bfd = NULL;
if (priv->size != 0)
{
int low, mid, high;
char *ptr;
CORE_ADDR pdr_pc;
low = 0;
high = priv->size / 32;
do
{
mid = (low + high) / 2;
ptr = priv->contents + mid * 32;
pdr_pc = bfd_get_signed_32 (sec->objfile->obfd, ptr);
pdr_pc += objfile_text_section_offset (sec->objfile);
if (pdr_pc > pc)
high = mid;
else
low = mid + 1;
}
while (low != high);
if (low > 0)
{
ptr = priv->contents + (low - 1) * 32;
pdr_pc = bfd_get_signed_32 (sec->objfile->obfd, ptr);
pdr_pc += objfile_text_section_offset (sec->objfile);
}
if (pc >= pdr_pc && pdr_pc >= startaddr)
{
struct symbol *sym = find_pc_function (pc);
if (addrptr)
*addrptr = pdr_pc;
proc_desc = (struct mdebug_extra_func_info *)
obstack_alloc (&sec->objfile->objfile_obstack,
sizeof (struct mdebug_extra_func_info));
PROC_LOW_ADDR (proc_desc) = pdr_pc;
PROC_FRAME_OFFSET (proc_desc)
= bfd_get_32 (sec->objfile->obfd, ptr + 20);
PROC_FRAME_REG (proc_desc) = bfd_get_32 (sec->objfile->obfd,
ptr + 24);
PROC_REG_MASK (proc_desc) = bfd_get_32 (sec->objfile->obfd,
ptr + 4);
PROC_FREG_MASK (proc_desc) = bfd_get_32 (sec->objfile->obfd,
ptr + 12);
PROC_REG_OFFSET (proc_desc) = bfd_get_32 (sec->objfile->obfd,
ptr + 8);
PROC_FREG_OFFSET (proc_desc)
= bfd_get_32 (sec->objfile->obfd, ptr + 16);
PROC_PC_REG (proc_desc) = bfd_get_32 (sec->objfile->obfd,
ptr + 28);
proc_desc->pdr.isym = (long) sym;
return proc_desc;
}
}
}
if (b == NULL)
return NULL;
if (startaddr > BLOCK_LOWEST_PC (b))
{
return NULL;
}
sym = lookup_symbol (MDEBUG_EFI_SYMBOL_NAME, b, LABEL_DOMAIN, 0, NULL);
if (sym)
{
proc_desc = (struct mdebug_extra_func_info *) SYMBOL_VALUE (sym);
if (PROC_FRAME_REG (proc_desc) == -1)
return NULL;
else
return proc_desc;
}
else
return NULL;
}
struct mips_frame_cache
{
CORE_ADDR base;
struct trad_frame_saved_reg *saved_regs;
};
static struct mips_frame_cache *
mips_mdebug_frame_cache (struct frame_info *next_frame, void **this_cache)
{
CORE_ADDR startaddr = 0;
struct mdebug_extra_func_info *proc_desc;
struct mips_frame_cache *cache;
struct gdbarch *gdbarch = get_frame_arch (next_frame);
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int kernel_trap;
unsigned long gen_mask, float_mask;
if ((*this_cache) != NULL)
return (*this_cache);
cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
(*this_cache) = cache;
cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
proc_desc = non_heuristic_proc_desc (frame_pc_unwind (next_frame),
&startaddr);
gdb_assert (proc_desc != NULL);
cache->base = (frame_unwind_register_signed (next_frame, NUM_REGS + PROC_FRAME_REG (proc_desc))
+ PROC_FRAME_OFFSET (proc_desc));
kernel_trap = PROC_REG_MASK (proc_desc) & 1;
gen_mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK (proc_desc);
float_mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK (proc_desc);
gdb_assert (!in_prologue (frame_pc_unwind (next_frame), PROC_LOW_ADDR (proc_desc))
|| kernel_trap);
{
CORE_ADDR reg_position = (cache->base + PROC_REG_OFFSET (proc_desc));
int ireg;
for (ireg = MIPS_NUMREGS - 1; gen_mask; --ireg, gen_mask <<= 1)
if (gen_mask & 0x80000000)
{
cache->saved_regs[NUM_REGS + ireg].addr = reg_position;
reg_position -= mips_abi_regsize (gdbarch);
}
}
{
CORE_ADDR reg_position = (cache->base
+ PROC_FREG_OFFSET (proc_desc));
int ireg;
for (ireg = MIPS_NUMREGS - 1; float_mask; --ireg, float_mask <<= 1)
if (float_mask & 0x80000000)
{
if (mips_abi_regsize (gdbarch) == 4
&& TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
{
if ((ireg & 1))
cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->fp0 + ireg]
.addr = reg_position - mips_abi_regsize (gdbarch);
else
cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->fp0 + ireg]
.addr = reg_position + mips_abi_regsize (gdbarch);
}
else
cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->fp0 + ireg]
.addr = reg_position;
reg_position -= mips_abi_regsize (gdbarch);
}
cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->pc]
= cache->saved_regs[NUM_REGS + MIPS_RA_REGNUM];
}
trad_frame_set_value (cache->saved_regs, NUM_REGS + MIPS_SP_REGNUM, cache->base);
return (*this_cache);
}
static void
mips_mdebug_frame_this_id (struct frame_info *next_frame, void **this_cache,
struct frame_id *this_id)
{
struct mips_frame_cache *info = mips_mdebug_frame_cache (next_frame,
this_cache);
(*this_id) = frame_id_build (info->base, frame_func_unwind (next_frame));
}
static void
mips_mdebug_frame_prev_register (struct frame_info *next_frame,
void **this_cache,
int regnum, enum opt_state *optimizedp,
enum lval_type *lvalp, CORE_ADDR *addrp,
int *realnump, void *valuep)
{
struct mips_frame_cache *info = mips_mdebug_frame_cache (next_frame,
this_cache);
trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
optimizedp, lvalp, addrp, realnump, valuep);
}
static const struct frame_unwind mips_mdebug_frame_unwind =
{
NORMAL_FRAME,
mips_mdebug_frame_this_id,
mips_mdebug_frame_prev_register
};
static const struct frame_unwind *
mips_mdebug_frame_sniffer (struct frame_info *next_frame)
{
CORE_ADDR pc = frame_pc_unwind (next_frame);
CORE_ADDR startaddr = 0;
struct mdebug_extra_func_info *proc_desc;
int kernel_trap;
if (mips_pc_is_mips16 (pc))
return NULL;
proc_desc = non_heuristic_proc_desc (pc, &startaddr);
if (proc_desc == NULL)
return NULL;
kernel_trap = PROC_REG_MASK (proc_desc) & 1;
if (kernel_trap)
return &mips_mdebug_frame_unwind;
if (!in_prologue (pc, PROC_LOW_ADDR (proc_desc)))
return &mips_mdebug_frame_unwind;
return NULL;
}
static CORE_ADDR
mips_mdebug_frame_base_address (struct frame_info *next_frame,
void **this_cache)
{
struct mips_frame_cache *info = mips_mdebug_frame_cache (next_frame,
this_cache);
return info->base;
}
static const struct frame_base mips_mdebug_frame_base = {
&mips_mdebug_frame_unwind,
mips_mdebug_frame_base_address,
mips_mdebug_frame_base_address,
mips_mdebug_frame_base_address
};
static const struct frame_base *
mips_mdebug_frame_base_sniffer (struct frame_info *next_frame)
{
if (mips_mdebug_frame_sniffer (next_frame) != NULL)
return &mips_mdebug_frame_base;
else
return NULL;
}
void
mips_mdebug_append_sniffers (struct gdbarch *gdbarch)
{
frame_unwind_append_sniffer (gdbarch, mips_mdebug_frame_sniffer);
frame_base_append_sniffer (gdbarch, mips_mdebug_frame_base_sniffer);
}
extern void _initialize_mips_mdebug_tdep (void);
void
_initialize_mips_mdebug_tdep (void)
{
mips_pdr_data = register_objfile_data ();
}