sim: unify symbol table handling

The common sim tracing code already handles loading and tracking of
symbols from the target program so that it can show symbol info in
trace/disassembly calls.  Once we touch up the trace code and add a
few API callbacks, ports don't need to do loading and searching of
symbol tables themselves anymore.
This commit is contained in:
Mike Frysinger 2016-01-21 22:17:59 -05:00
parent 31925464a8
commit 5357150c97
17 changed files with 164 additions and 170 deletions

View File

@ -1,3 +1,22 @@
2016-08-15 Mike Frysinger <vapier@gentoo.org>
* interp.c: Include bfd.h.
(symcount, symtab, aarch64_get_sym_value): Delete.
(remove_useless_symbols): Change count type to long.
(aarch64_get_func): Add SIM_DESC to arg list. Add symcount
and symtab local variables.
(sim_create_inferior): Delete storage. Replace symbol code
with a call to trace_load_symbols.
* memory.c: Delete bfd.h, elf/internal.h, and elf/common.h
includes.
(aarch64_get_heap_start): Change aarch64_get_sym_value to
trace_sym_value.
* memory.h: Delete bfd.h include.
(mem_add_blk): Delete unused prototype.
* simulator.c (bl, blr): Pass SIM_DESC to aarch64_get_func.
* simulator.c (aarch64_get_func): Add SIM_DESC to arg list.
(aarch64_get_sym_value): Delete.
2016-08-12 Nick Clifton <nickc@redhat.com>
* simulator.c (aarch64_step): Revert pervious delta.

View File

@ -28,6 +28,7 @@
#include <stdlib.h>
#include "ansidecl.h"
#include "bfd.h"
#include "gdb/callback.h"
#include "gdb/remote-sim.h"
#include "gdb/signals.h"
@ -38,15 +39,12 @@
#include "memory.h"
#include "simulator.h"
static unsigned long symcount = 0;
static asymbol ** symtab = NULL;
/* Filter out (in place) symbols that are useless for disassembly.
COUNT is the number of elements in SYMBOLS.
Return the number of useful symbols. */
static unsigned long
remove_useless_symbols (asymbol **symbols, unsigned long count)
static long
remove_useless_symbols (asymbol **symbols, long count)
{
asymbol **in_ptr = symbols;
asymbol **out_ptr = symbols;
@ -87,8 +85,10 @@ compare_symbols (const void *ap, const void *bp)
/* Find the name of the function at ADDR. */
const char *
aarch64_get_func (uint64_t addr)
aarch64_get_func (SIM_DESC sd, uint64_t addr)
{
long symcount = STATE_PROG_SYMS_COUNT (sd);
asymbol **symtab = STATE_PROG_SYMS (sd);
int min, max;
min = -1;
@ -118,24 +118,11 @@ aarch64_get_func (uint64_t addr)
return "";
}
uint64_t
aarch64_get_sym_value (const char *name)
{
unsigned long i;
for (i = 0; i < symcount; i++)
if (strcmp (bfd_asymbol_name (symtab[i]), name) == 0)
return bfd_asymbol_value (symtab[i]);
return 0;
}
SIM_RC
sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
char * const *argv, char * const *env)
{
sim_cpu *cpu = STATE_CPU (sd, 0);
long storage = 0;
bfd_vma addr = 0;
if (abfd != NULL)
@ -154,14 +141,13 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
STATE_PROG_ARGV (sd) = dupargv (argv);
}
if (abfd != NULL)
storage = bfd_get_symtab_upper_bound (abfd);
if (storage > 0)
if (trace_load_symbols (sd))
{
symtab = (asymbol **) xmalloc (storage);
symcount = bfd_canonicalize_symtab (abfd, symtab);
symcount = remove_useless_symbols (symtab, symcount);
qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
STATE_PROG_SYMS_COUNT (sd) =
remove_useless_symbols (STATE_PROG_SYMS (sd),
STATE_PROG_SYMS_COUNT (sd));
qsort (STATE_PROG_SYMS (sd), STATE_PROG_SYMS_COUNT (sd),
sizeof (asymbol *), compare_symbols);
}
aarch64_init (cpu, addr);

View File

@ -25,10 +25,7 @@
#include <stdlib.h>
#include <string.h>
#include "bfd.h"
#include "libiberty.h"
#include "elf/internal.h"
#include "elf/common.h"
#include "memory.h"
#include "simulator.h"
@ -163,10 +160,10 @@ aarch64_get_mem_ptr (sim_cpu *cpu, uint64_t address)
uint64_t
aarch64_get_heap_start (sim_cpu *cpu)
{
uint64_t heap = aarch64_get_sym_value ("end");
uint64_t heap = trace_sym_value (CPU_STATE (cpu), "end");
if (heap == 0)
heap = aarch64_get_sym_value ("_end");
heap = trace_sym_value (CPU_STATE (cpu), "_end");
if (heap == 0)
{
heap = STACK_TOP - 0x100000;

View File

@ -23,7 +23,6 @@
#define _MEMORY_H
#include <sys/types.h>
#include "bfd.h"
#include "simulator.h"
extern void aarch64_get_mem_long_double (sim_cpu *, uint64_t, FRegister *);
@ -53,6 +52,4 @@ extern void aarch64_set_mem_s8 (sim_cpu *, uint64_t, int8_t);
extern uint64_t aarch64_get_heap_start (sim_cpu *);
extern uint64_t aarch64_get_stack_start (sim_cpu *);
extern void mem_add_blk (sim_cpu *, uint64_t, char *, uint64_t, bfd_boolean);
#endif /* _MEMORY_H */

View File

@ -13163,7 +13163,8 @@ bl (sim_cpu *cpu, int32_t offset)
" %*scall %" PRIx64 " [%s]"
" [args: %" PRIx64 " %" PRIx64 " %" PRIx64 "]",
stack_depth, " ", aarch64_get_next_PC (cpu),
aarch64_get_func (aarch64_get_next_PC (cpu)),
aarch64_get_func (CPU_STATE (cpu),
aarch64_get_next_PC (cpu)),
aarch64_get_reg_u64 (cpu, 0, NO_SP),
aarch64_get_reg_u64 (cpu, 1, NO_SP),
aarch64_get_reg_u64 (cpu, 2, NO_SP)
@ -13202,7 +13203,8 @@ blr (sim_cpu *cpu)
" %*scall %" PRIx64 " [%s]"
" [args: %" PRIx64 " %" PRIx64 " %" PRIx64 "]",
stack_depth, " ", aarch64_get_next_PC (cpu),
aarch64_get_func (aarch64_get_next_PC (cpu)),
aarch64_get_func (CPU_STATE (cpu),
aarch64_get_next_PC (cpu)),
aarch64_get_reg_u64 (cpu, 0, NO_SP),
aarch64_get_reg_u64 (cpu, 1, NO_SP),
aarch64_get_reg_u64 (cpu, 2, NO_SP)

View File

@ -46,8 +46,7 @@ extern void aarch64_init (sim_cpu *, uint64_t);
hit an error. */
extern void aarch64_run (SIM_DESC);
extern const char * aarch64_get_func (uint64_t);
extern uint64_t aarch64_get_sym_value (const char *);
extern const char * aarch64_get_func (SIM_DESC, uint64_t);
extern void aarch64_init_LIT_table (void);
#endif /* _SIMULATOR_H */

View File

@ -1,3 +1,15 @@
2016-08-15 Mike Frysinger <vapier@gentoo.org>
* sim-base.h (sim_state_base): Add prog_syms_count.
(STATE_PROG_SYMS_COUNT): Define.
* sim-trace.c (trace_uninstall): Free STATE_PROG_SYMS memory.
(trace_load_symbols): New function.
(trace_sym_value): Likewise.
(trace_prefix): Change STATE_CPU(cpu) to sd. Replace symbol
loading logic with a call to trace_load_symbols.
* sim-trace.h (trace_load_symbols, trace_sym_value): New
prototypes.
2016-08-13 Mike Frysinger <vapier@gentoo.org>
* cgen-types.h (mode_names): Mark const.

View File

@ -160,6 +160,10 @@ typedef struct {
struct bfd_symbol **prog_syms;
#define STATE_PROG_SYMS(sd) ((sd)->base.prog_syms)
/* Number of prog_syms symbols. */
long prog_syms_count;
#define STATE_PROG_SYMS_COUNT(sd) ((sd)->base.prog_syms_count)
/* The program's text section. */
struct bfd_section *text_section;
/* Starting and ending text section addresses from the bfd. */

View File

@ -510,6 +510,9 @@ trace_uninstall (SIM_DESC sd)
fclose (cfile);
}
}
if (STATE_PROG_SYMS (sd))
free (STATE_PROG_SYMS (sd));
}
/* compute the nr of trace data units consumed by data */
@ -685,6 +688,57 @@ trace_results (SIM_DESC sd,
trace_printf (sd, cpu, "\n");
}
int
trace_load_symbols (SIM_DESC sd)
{
bfd *abfd;
asymbol **asymbols;
long symsize;
long symbol_count;
/* Already loaded, so nothing to do. */
if (STATE_PROG_SYMS (sd))
return 1;
abfd = STATE_PROG_BFD (sd);
if (abfd == NULL)
return 0;
symsize = bfd_get_symtab_upper_bound (abfd);
if (symsize < 0)
return 0;
asymbols = xmalloc (symsize);
symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
if (symbol_count < 0)
{
free (asymbols);
return 0;
}
STATE_PROG_SYMS (sd) = asymbols;
STATE_PROG_SYMS_COUNT (sd) = symbol_count;
return 1;
}
bfd_vma
trace_sym_value (SIM_DESC sd, const char *name)
{
asymbol **asymbols;
long i;
if (!trace_load_symbols (sd))
return -1;
asymbols = STATE_PROG_SYMS (sd);
for (i = 0; i < STATE_PROG_SYMS_COUNT (sd); ++i)
if (strcmp (asymbols[i]->name, name) == 0)
return bfd_asymbol_value (asymbols[i]);
return -1;
}
void
trace_prefix (SIM_DESC sd,
sim_cpu *cpu,
@ -744,9 +798,9 @@ trace_prefix (SIM_DESC sd,
{
char buf[256];
buf[0] = 0;
if (STATE_TEXT_SECTION (CPU_STATE (cpu))
&& pc >= STATE_TEXT_START (CPU_STATE (cpu))
&& pc < STATE_TEXT_END (CPU_STATE (cpu)))
if (STATE_TEXT_SECTION (sd)
&& pc >= STATE_TEXT_START (sd)
&& pc < STATE_TEXT_END (sd))
{
const char *pc_filename = (const char *)0;
const char *pc_function = (const char *)0;
@ -754,31 +808,14 @@ trace_prefix (SIM_DESC sd,
bfd *abfd;
asymbol **asymbols;
abfd = STATE_PROG_BFD (CPU_STATE (cpu));
asymbols = STATE_PROG_SYMS (CPU_STATE (cpu));
if (asymbols == NULL)
{
long symsize;
long symbol_count;
if (!trace_load_symbols (sd))
sim_engine_abort (sd, cpu, cia, "could not load symbols");
symsize = bfd_get_symtab_upper_bound (abfd);
if (symsize < 0)
{
sim_engine_abort (sd, cpu, cia, "could not read symbols");
}
asymbols = (asymbol **) xmalloc (symsize);
symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
if (symbol_count < 0)
{
sim_engine_abort (sd, cpu, cia, "could not canonicalize symbols");
}
STATE_PROG_SYMS (CPU_STATE (cpu)) = asymbols;
}
abfd = STATE_PROG_BFD (sd);
asymbols = STATE_PROG_SYMS (sd);
if (bfd_find_nearest_line (abfd,
STATE_TEXT_SECTION (CPU_STATE (cpu)),
asymbols,
pc - STATE_TEXT_START (CPU_STATE (cpu)),
if (bfd_find_nearest_line (abfd, STATE_TEXT_SECTION (sd), asymbols,
pc - STATE_TEXT_START (sd),
&pc_filename, &pc_function, &pc_linenum))
{
char *p = buf;

View File

@ -671,6 +671,10 @@ extern void trace_vprintf (SIM_DESC, sim_cpu *, const char *, va_list);
/* Non-zero if "--debug-insn" specified. */
#define DEBUG_INSN_P(cpu) DEBUG_P (cpu, DEBUG_INSN_IDX)
/* Symbol related helpers. */
int trace_load_symbols (SIM_DESC);
bfd_vma trace_sym_value (SIM_DESC, const char *name);
extern void sim_debug_printf (sim_cpu *, const char *, ...)
__attribute__((format (printf, 2, 3)));

View File

@ -1,3 +1,9 @@
2016-08-15 Mike Frysinger <vapier@gentoo.org>
* sim-if.c (find_limit): Change prototype to take a SIM_DESC.
Replace symbol lookup code with a call to trace_sym_value.
(sim_open): Update find_limit call.
2016-01-10 Mike Frysinger <vapier@gentoo.org>
* config.in, configure: Regenerate.

View File

@ -71,27 +71,15 @@ find_base (bfd *prog_bfd)
}
static unsigned long
find_limit (bfd *prog_bfd)
find_limit (SIM_DESC sd)
{
struct bfd_symbol **asymbols;
long symsize;
long symbol_count;
long s;
bfd_vma addr;
symsize = bfd_get_symtab_upper_bound (prog_bfd);
if (symsize < 0)
return 0;
asymbols = (asymbol **) xmalloc (symsize);
symbol_count = bfd_canonicalize_symtab (prog_bfd, asymbols);
if (symbol_count < 0)
addr = trace_sym_value (sd, "_fstack");
if (addr == -1)
return 0;
for (s = 0; s < symbol_count; s++)
{
if (!strcmp (asymbols[s]->name, "_fstack"))
return (asymbols[s]->value + 65536) & ~(0xffffUL);
}
return 0;
return (addr + 65536) & ~(0xffffUL);
}
/* Create an instance of the simulator. */
@ -159,7 +147,7 @@ sim_open (kind, callback, abfd, argv)
{
/* It doesn't, so we should try to allocate enough memory to hold program. */
base = find_base (STATE_PROG_BFD (sd));
limit = find_limit (STATE_PROG_BFD (sd));
limit = find_limit (sd);
if (limit == 0)
{
sim_io_eprintf (sd,

View File

@ -1,3 +1,9 @@
2016-08-15 Mike Frysinger <vapier@gentoo.org>
* interp.c (sim_get_bank_parameters): Delete abfd arg.
Replace all symbol lookup code with calls to trace_sym_value.
(sim_prepare_for_program): Update sim_get_bank_parameters call.
2016-08-13 Mike Frysinger <vapier@gentoo.org>
* dv-m68hc11.c (m68hc11cpu_port_event): Adjust cpu prototype style.

View File

@ -292,50 +292,25 @@ sim_hw_configure (SIM_DESC sd)
/* Get the memory bank parameters by looking at the global symbols
defined by the linker. */
static int
sim_get_bank_parameters (SIM_DESC sd, bfd* abfd)
sim_get_bank_parameters (SIM_DESC sd)
{
sim_cpu *cpu;
long symsize;
long symbol_count, i;
unsigned size;
asymbol** asymbols;
asymbol** current;
bfd_vma addr;
cpu = STATE_CPU (sd, 0);
symsize = bfd_get_symtab_upper_bound (abfd);
if (symsize < 0)
{
sim_io_eprintf (sd, "Cannot read symbols of program");
return 0;
}
asymbols = (asymbol **) xmalloc (symsize);
symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
if (symbol_count < 0)
{
sim_io_eprintf (sd, "Cannot read symbols of program");
return 0;
}
addr = trace_sym_value (sd, BFD_M68HC11_BANK_START_NAME);
if (addr != -1)
cpu->bank_start = addr;
size = 0;
for (i = 0, current = asymbols; i < symbol_count; i++, current++)
{
const char* name = bfd_asymbol_name (*current);
size = trace_sym_value (sd, BFD_M68HC11_BANK_SIZE_NAME);
if (size == -1)
size = 0;
if (strcmp (name, BFD_M68HC11_BANK_START_NAME) == 0)
{
cpu->bank_start = bfd_asymbol_value (*current);
}
else if (strcmp (name, BFD_M68HC11_BANK_SIZE_NAME) == 0)
{
size = bfd_asymbol_value (*current);
}
else if (strcmp (name, BFD_M68HC11_BANK_VIRTUAL_NAME) == 0)
{
cpu->bank_virtual = bfd_asymbol_value (*current);
}
}
free (asymbols);
addr = trace_sym_value (sd, BFD_M68HC11_BANK_VIRTUAL_NAME);
if (addr != -1)
cpu->bank_virtual = addr;
cpu->bank_end = cpu->bank_start + size;
cpu->bank_shift = 0;
@ -387,7 +362,7 @@ sim_prepare_for_program (SIM_DESC sd, bfd* abfd)
if (elf_flags & E_M68HC12_BANKS)
{
if (sim_get_bank_parameters (sd, abfd) != 0)
if (sim_get_bank_parameters (sd) != 0)
sim_io_eprintf (sd, "Memory bank parameters are not initialized\n");
}
}

View File

@ -1,3 +1,13 @@
2016-08-15 Mike Frysinger <vapier@gentoo.org>
* msp430-sim.c: Delete bfd.h include.
(lookup_symbol, msp430_sim_close): Delete.
(sim_open): Change lookup_symbol to trace_sym_value.
* sim-main.h (struct sim_state): Delete symbol_table and
number_of_symbols.
(STATE_SYMBOL_TABLE, STATE_NUM_SYMBOLS, msp430_sim_close,
SIM_CLOSE_HOOK): Delete.
2016-01-10 Mike Frysinger <vapier@gentoo.org>
* config.in, configure: Regenerate.

View File

@ -26,7 +26,6 @@
#include <inttypes.h>
#include <unistd.h>
#include <assert.h>
#include "bfd.h"
#include "opcode/msp430-decode.h"
#include "sim-main.h"
#include "sim-syscall.h"
@ -44,39 +43,6 @@ msp430_pc_store (SIM_CPU *cpu, sim_cia newpc)
cpu->state.regs[0] = newpc;
}
static long
lookup_symbol (SIM_DESC sd, const char *name)
{
struct bfd *abfd = STATE_PROG_BFD (sd);
asymbol **symbol_table = STATE_SYMBOL_TABLE (sd);
long number_of_symbols = STATE_NUM_SYMBOLS (sd);
long i;
if (abfd == NULL)
return -1;
if (symbol_table == NULL)
{
long storage_needed;
storage_needed = bfd_get_symtab_upper_bound (abfd);
if (storage_needed <= 0)
return -1;
STATE_SYMBOL_TABLE (sd) = symbol_table = xmalloc (storage_needed);
STATE_NUM_SYMBOLS (sd) = number_of_symbols =
bfd_canonicalize_symtab (abfd, symbol_table);
}
for (i = 0; i < number_of_symbols; i++)
if (strcmp (symbol_table[i]->name, name) == 0)
{
long val = symbol_table[i]->section->vma + symbol_table[i]->value;
return val;
}
return -1;
}
static int
msp430_reg_fetch (SIM_CPU *cpu, int regno, unsigned char *buf, int len)
{
@ -207,20 +173,14 @@ sim_open (SIM_OPEN_KIND kind,
assert (MAX_NR_PROCESSORS == 1);
msp430_initialize_cpu (sd, MSP430_CPU (sd));
MSP430_CPU (sd)->state.cio_breakpoint = lookup_symbol (sd, "C$$IO$$");
MSP430_CPU (sd)->state.cio_buffer = lookup_symbol (sd, "__CIOBUF__");
MSP430_CPU (sd)->state.cio_breakpoint = trace_sym_value (sd, "C$$IO$$");
MSP430_CPU (sd)->state.cio_buffer = trace_sym_value (sd, "__CIOBUF__");
if (MSP430_CPU (sd)->state.cio_buffer == -1)
MSP430_CPU (sd)->state.cio_buffer = lookup_symbol (sd, "_CIOBUF_");
MSP430_CPU (sd)->state.cio_buffer = trace_sym_value (sd, "_CIOBUF_");
return sd;
}
void
msp430_sim_close (SIM_DESC sd, int quitting)
{
free (STATE_SYMBOL_TABLE (sd));
}
SIM_RC
sim_create_inferior (SIM_DESC sd,
struct bfd *abfd,

View File

@ -37,11 +37,6 @@ struct sim_state
{
sim_cpu *cpu[MAX_NR_PROCESSORS];
asymbol **symbol_table;
long number_of_symbols;
#define STATE_SYMBOL_TABLE(sd) ((sd)->symbol_table)
#define STATE_NUM_SYMBOLS(sd) ((sd)->number_of_symbols)
/* Simulator specific members. */
sim_state_base base;
};
@ -54,7 +49,4 @@ struct sim_state
#include "sim-engine.h"
#include "sim-options.h"
extern void msp430_sim_close (SIM_DESC sd, int quitting);
#define SIM_CLOSE_HOOK(...) msp430_sim_close (__VA_ARGS__)
#endif /* _MSP430_MAIN_SIM_H_ */