X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=sim%2Fcris%2Fsim-if.c;h=67e8d691591ca8606ec12f8b3e4463447963a7e2;hb=a350efd4fb368a35ada608f6bc26ccd3bed0ae6b;hp=9f26abf2ade45449ff2e26222e853820b8d55226;hpb=e4d013fc0f3df3200be2dca48ed2e330e8a4ddf9;p=deliverable%2Fbinutils-gdb.git diff --git a/sim/cris/sim-if.c b/sim/cris/sim-if.c index 9f26abf2ad..67e8d69159 100644 --- a/sim/cris/sim-if.c +++ b/sim/cris/sim-if.c @@ -1,6 +1,5 @@ /* Main simulator entry points specific to the CRIS. - Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 - Free Software Foundation, Inc. + Copyright (C) 2004-2020 Free Software Foundation, Inc. Contributed by Axis Communications. This file is part of the GNU simulators. @@ -21,6 +20,7 @@ along with this program. If not, see . */ /* Based on the fr30 file, mixing in bits from the i960 and pruning of dead code. */ +#include "config.h" #include "libiberty.h" #include "bfd.h" #include "elf-bfd.h" @@ -29,6 +29,7 @@ along with this program. If not, see . */ #ifdef HAVE_STDLIB_H #include #endif +#include #include "sim-options.h" #include "dis-asm.h" @@ -67,19 +68,26 @@ static char cris_bare_iron = 0; /* Whether 0x9000000xx have simulator-specific meanings. */ char cris_have_900000xxif = 0; +/* Used to optionally override the default start address of the + simulation. */ +static USI cris_start_address = 0xffffffffu; + +/* Used to optionally add offsets to the loaded image and its start + address. (Not used for the interpreter of dynamically loaded + programs or the DSO:s.) */ +static int cris_program_offset = 0; + /* What to do when we face a more or less unknown syscall. */ enum cris_unknown_syscall_action_type cris_unknown_syscall_action = CRIS_USYSC_MSG_STOP; -/* Records simulator descriptor so utilities like cris_dump_regs can be - called from gdb. */ -SIM_DESC current_state; - /* CRIS-specific options. */ typedef enum { OPTION_CRIS_STATS = OPTION_START, OPTION_CRIS_TRACE, OPTION_CRIS_NAKED, + OPTION_CRIS_PROGRAM_OFFSET, + OPTION_CRIS_STARTADDR, OPTION_CRIS_900000XXIF, OPTION_CRIS_UNKNOWN_SYSCALL } CRIS_OPTIONS; @@ -104,20 +112,17 @@ static const OPTION cris_options[] = OPTION_CRIS_UNKNOWN_SYSCALL}, '\0', "stop|enosys|enosys-quiet", "Action at an unknown system call", cris_option_handler, NULL }, + { {"cris-program-offset", required_argument, NULL, + OPTION_CRIS_PROGRAM_OFFSET}, + '\0', "OFFSET", + "Offset image addresses and default start address of a program", + cris_option_handler }, + { {"cris-start-address", required_argument, NULL, OPTION_CRIS_STARTADDR}, + '\0', "ADDRESS", "Set start address", + cris_option_handler }, { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL } }; -/* Add the CRIS-specific option list to the simulator. */ - -SIM_RC -cris_option_install (SIM_DESC sd) -{ - SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); - if (sim_add_option_table (sd, NULL, cris_options) != SIM_RC_OK) - return SIM_RC_FAIL; - return SIM_RC_OK; -} - /* Handle CRIS-specific options. */ static SIM_RC @@ -130,6 +135,7 @@ cris_option_handler (SIM_DESC sd, sim_cpu *cpu ATTRIBUTE_UNUSED, int opt, to the module-specific CPU data when we store things in the cpu-specific structure. */ char *tracefp = STATE_TRACE_FLAGS (sd); + char *chp = arg; switch ((CRIS_OPTIONS) opt) { @@ -173,6 +179,30 @@ cris_option_handler (SIM_DESC sd, sim_cpu *cpu ATTRIBUTE_UNUSED, int opt, cris_have_900000xxif = 1; break; + case OPTION_CRIS_STARTADDR: + errno = 0; + cris_start_address = (USI) strtoul (chp, &chp, 0); + + if (errno != 0 || *chp != 0) + { + sim_io_eprintf (sd, "Invalid option `--cris-start-address=%s'\n", + arg); + return SIM_RC_FAIL; + } + break; + + case OPTION_CRIS_PROGRAM_OFFSET: + errno = 0; + cris_program_offset = (int) strtol (chp, &chp, 0); + + if (errno != 0 || *chp != 0) + { + sim_io_eprintf (sd, "Invalid option `--cris-program-offset=%s'\n", + arg); + return SIM_RC_FAIL; + } + break; + case OPTION_CRIS_UNKNOWN_SYSCALL: if (strcmp (arg, "enosys") == 0) cris_unknown_syscall_action = CRIS_USYSC_MSG_ENOSYS; @@ -199,32 +229,6 @@ cris_option_handler (SIM_DESC sd, sim_cpu *cpu ATTRIBUTE_UNUSED, int opt, return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on"); } -/* FIXME: Remove these, globalize those in sim-load.c, move elsewhere. */ - -static void -xprintf (host_callback *callback, const char *fmt, ...) -{ - va_list ap; - - va_start (ap, fmt); - - (*callback->vprintf_filtered) (callback, fmt, ap); - - va_end (ap); -} - -static void -eprintf (host_callback *callback, const char *fmt, ...) -{ - va_list ap; - - va_start (ap, fmt); - - (*callback->evprintf_filtered) (callback, fmt, ap); - - va_end (ap); -} - /* An ELF-specific simplified ../common/sim-load.c:sim_load_file, using the program headers, not sections, in order to make sure that the program headers themeselves are also loaded. The caller is @@ -237,7 +241,6 @@ cris_load_elf_file (SIM_DESC sd, struct bfd *abfd, sim_write_fn do_write) int n_hdrs; int i; bfd_boolean verbose = STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG; - host_callback *callback = STATE_CALLBACK (sd); phdr = elf_tdata (abfd)->phdr; n_hdrs = elf_elfheader (abfd)->e_phnum; @@ -256,24 +259,24 @@ cris_load_elf_file (SIM_DESC sd, struct bfd *abfd, sim_write_fn do_write) buf = xmalloc (phdr[i].p_filesz); if (verbose) - xprintf (callback, "Loading segment at 0x%lx, size 0x%lx\n", - lma, phdr[i].p_filesz); + sim_io_printf (sd, "Loading segment at 0x%lx, size 0x%lx\n", + lma, phdr[i].p_filesz); if (bfd_seek (abfd, phdr[i].p_offset, SEEK_SET) != 0 || (bfd_bread (buf, phdr[i].p_filesz, abfd) != phdr[i].p_filesz)) { - eprintf (callback, - "%s: could not read segment at 0x%lx, size 0x%lx\n", - STATE_MY_NAME (sd), lma, phdr[i].p_filesz); + sim_io_eprintf (sd, + "%s: could not read segment at 0x%lx, size 0x%lx\n", + STATE_MY_NAME (sd), lma, phdr[i].p_filesz); free (buf); return FALSE; } if (do_write (sd, lma, buf, phdr[i].p_filesz) != phdr[i].p_filesz) { - eprintf (callback, - "%s: could not load segment at 0x%lx, size 0x%lx\n", - STATE_MY_NAME (sd), lma, phdr[i].p_filesz); + sim_io_eprintf (sd, + "%s: could not load segment at 0x%lx, size 0x%lx\n", + STATE_MY_NAME (sd), lma, phdr[i].p_filesz); free (buf); return FALSE; } @@ -284,51 +287,69 @@ cris_load_elf_file (SIM_DESC sd, struct bfd *abfd, sim_write_fn do_write) return TRUE; } -/* Replacement for ../common/sim-hload.c:sim_load, so we can treat ELF - files differently. */ +/* Cover function of sim_state_free to free the cpu buffers as well. */ -SIM_RC -sim_load (SIM_DESC sd, char *prog_name, struct bfd *prog_bfd, - int from_tty ATTRIBUTE_UNUSED) +static void +free_state (SIM_DESC sd) { - bfd *result_bfd; + if (STATE_MODULES (sd) != NULL) + sim_module_uninstall (sd); + sim_cpu_free_all (sd); + sim_state_free (sd); +} + +/* Helper struct for cris_set_section_offset_iterator. */ + +struct offsetinfo +{ + SIM_DESC sd; + int offset; +}; + +/* BFD section iterator to offset the LMA and VMA. */ - if (bfd_get_flavour (prog_bfd) != bfd_target_elf_flavour) +static void +cris_set_section_offset_iterator (bfd *abfd, asection *s, void *vp) +{ + struct offsetinfo *p = (struct offsetinfo *) vp; + SIM_DESC sd = p->sd; + int offset = p->offset; + + if ((bfd_section_flags (s) & SEC_ALLOC)) { - SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); - if (sim_analyze_program (sd, prog_name, prog_bfd) != SIM_RC_OK) - return SIM_RC_FAIL; - SIM_ASSERT (STATE_PROG_BFD (sd) != NULL); - - result_bfd = sim_load_file (sd, STATE_MY_NAME (sd), - STATE_CALLBACK (sd), - prog_name, - STATE_PROG_BFD (sd), - STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG, - STATE_LOAD_AT_LMA_P (sd), - sim_write); - if (result_bfd == NULL) - { - bfd_close (STATE_PROG_BFD (sd)); - STATE_PROG_BFD (sd) = NULL; - return SIM_RC_FAIL; - } - return SIM_RC_OK; + bfd_vma vma = bfd_section_vma (s); + + bfd_set_section_vma (s, vma + offset); } - return cris_load_elf_file (sd, prog_bfd, sim_write) - ? SIM_RC_OK : SIM_RC_FAIL; + /* This seems clumsy and inaccurate, but let's stick to doing it the + same way as sim_analyze_program for consistency. */ + if (strcmp (bfd_section_name (s), ".text") == 0) + STATE_TEXT_START (sd) = bfd_section_vma (s); } -/* Cover function of sim_state_free to free the cpu buffers as well. */ +/* Adjust the start-address, LMA and VMA of a SD. Must be called + after sim_analyze_program. */ static void -free_state (SIM_DESC sd) +cris_offset_sections (SIM_DESC sd, int offset) { - if (STATE_MODULES (sd) != NULL) - sim_module_uninstall (sd); - sim_cpu_free_all (sd); - sim_state_free (sd); + bfd_boolean ret; + struct bfd *abfd = STATE_PROG_BFD (sd); + asection *text; + struct offsetinfo oi; + + /* Only happens for usage error. */ + if (abfd == NULL) + return; + + oi.sd = sd; + oi.offset = offset; + + bfd_map_over_sections (abfd, cris_set_section_offset_iterator, &oi); + ret = bfd_set_start_address (abfd, bfd_get_start_address (abfd) + offset); + + STATE_START_ADDR (sd) = bfd_get_start_address (abfd); } /* BFD section iterator to find the highest and lowest allocated and @@ -339,10 +360,10 @@ get_progbounds_iterator (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *vp) { struct progbounds *pbp = (struct progbounds *) vp; - if ((bfd_get_section_flags (abfd, s) & SEC_ALLOC)) + if ((bfd_section_flags (s) & SEC_ALLOC)) { - bfd_size_type sec_size = bfd_get_section_size (s); - bfd_size_type sec_start = bfd_get_section_vma (abfd, s); + bfd_size_type sec_size = bfd_section_size (s); + bfd_size_type sec_start = bfd_section_vma (s); bfd_size_type sec_end = sec_start + sec_size; if (sec_end > pbp->endmem) @@ -351,7 +372,7 @@ get_progbounds_iterator (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *vp) if (sec_start < pbp->startmem) pbp->startmem = sec_start; - if ((bfd_get_section_flags (abfd, s) & SEC_LOAD)) + if ((bfd_section_flags (s) & SEC_LOAD)) { if (sec_end > pbp->end_loadmem) pbp->end_loadmem = sec_end; @@ -520,7 +541,7 @@ cris_handle_interpreter (SIM_DESC sd, struct bfd *abfd) if (ibfd == NULL) goto interpname_failed; - /* The interpreter is at leat something readable to BFD; make + /* The interpreter is at least something readable to BFD; make sure it's an ELF non-archive file. */ if (!bfd_check_format (ibfd, bfd_object) || bfd_get_flavour (ibfd) != bfd_target_elf_flavour) @@ -596,7 +617,7 @@ cris_handle_interpreter (SIM_DESC sd, struct bfd *abfd) SIM_DESC sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, - char **argv) + char * const *argv) { char c; int i; @@ -652,20 +673,19 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, return 0; } - /* getopt will print the error message so we just have to exit if this fails. - FIXME: Hmmm... in the case of gdb we need getopt to call - print_filtered. */ - if (sim_parse_args (sd, argv) != SIM_RC_OK) + /* Add the CRIS-specific option list to the simulator. */ + if (sim_add_option_table (sd, NULL, cris_options) != SIM_RC_OK) { free_state (sd); return 0; } - /* If we have a binary program, endianness-setting would not be taken - from elsewhere unfortunately, so set it here. At the time of this - writing, it isn't used until sim_config, but that might change so - set it here before memory is defined or touched. */ - current_target_byte_order = LITTLE_ENDIAN; + /* The parser will print an error message for us, so we silently return. */ + if (sim_parse_args (sd, argv) != SIM_RC_OK) + { + free_state (sd); + return 0; + } /* check for/establish the reference program image */ if (sim_analyze_program (sd, @@ -688,7 +708,12 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, if (abfd == NULL) abfd = STATE_PROG_BFD (sd); - if (bfd_get_arch (abfd) == bfd_arch_unknown) + /* Adjust the addresses of the program at this point. Unfortunately + this does not affect ELF program headers, so we have to handle + that separately. */ + cris_offset_sections (sd, cris_program_offset); + + if (abfd != NULL && bfd_get_arch (abfd) == bfd_arch_unknown) { if (STATE_PROG_ARGV (sd) != NULL) sim_io_eprintf (sd, "%s: `%s' is not a CRIS program\n", @@ -721,7 +746,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, specified. */ if (abfd != NULL && !cris_bare_iron) { - char *name = bfd_get_filename (abfd); + const char *name = bfd_get_filename (abfd); char **my_environ = GET_ENVIRON (); /* We use these maps to give the same behavior as the old xsim simulator. */ @@ -735,8 +760,6 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, int i; char **prog_argv = STATE_PROG_ARGV (sd); int my_argc = 0; - /* All CPU:s have the same memory map, apparently. */ - SIM_CPU *cpu = STATE_CPU (sd, 0); USI csp; bfd_byte buf[4]; @@ -763,7 +786,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, /* Note that the linux kernel does not correctly compute the storage needs for the static-exe AUX vector. */ - csp -= sizeof (auxv_entries) / sizeof (auxv_entries[0]) * 4 * 2; + csp -= ARRAY_SIZE (auxv_entries) * 4 * 2; csp -= (envc + 1) * 4; csp -= (my_argc + 1) * 4; @@ -800,7 +823,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, USI data_ = data; \ USI addr_ = addr; \ bfd_putl32 (data_, buf); \ - if (sim_core_write_buffer (sd, cpu, 0, buf, addr_, 4) != 4) \ + if (sim_core_write_buffer (sd, NULL, NULL_CIA, buf, addr_, 4) != 4)\ goto abandon_chip; \ } \ while (0) @@ -812,7 +835,8 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, { size_t strln = strlen (prog_argv[i]) + 1; - if (sim_core_write_buffer (sd, cpu, 0, prog_argv[i], epp, strln) + if (sim_core_write_buffer (sd, NULL, NULL_CIA, prog_argv[i], epp, + strln) != strln) goto abandon_chip; @@ -827,7 +851,8 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, { unsigned int strln = strlen (my_environ[i]) + 1; - if (sim_core_write_buffer (sd, cpu, 0, my_environ[i], epp, strln) + if (sim_core_write_buffer (sd, NULL, NULL_CIA, my_environ[i], epp, + strln) != strln) goto abandon_chip; @@ -849,7 +874,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, goto abandon_chip; if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) - for (i = 0; i < sizeof (auxv_entries) / sizeof (auxv_entries[0]); i++) + for (i = 0; i < ARRAY_SIZE (auxv_entries); i++) { write_dword (csp, auxv_entries[i].id); write_dword (csp + 4, @@ -867,18 +892,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, /* Allocate simulator I/O managed memory if none specified by user. */ if (cris_have_900000xxif) - { - if (sim_core_read_buffer (sd, NULL, read_map, &c, 0x90000000, 1) == 0) - sim_core_attach (sd, NULL, 0, access_write, 0, 0x90000000, 0x100, - 0, &cris_devices, NULL); - else - { - (*callback-> - printf_filtered) (callback, - "Seeing --cris-900000xx with memory defined there\n"); - goto abandon_chip; - } - } + sim_hw_parse (sd, "/core/%s/reg %#x %i", "cris_900000xx", 0x90000000, 0x100); /* Establish any remaining configuration options. */ if (sim_config (sd) != SIM_RC_OK) @@ -938,52 +952,41 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, Must be done after cris_cgen_cpu_open. */ cgen_init (sd); - /* Store in a global so things like cris_dump_regs can be invoked - from the gdb command line. */ - current_state = sd; - cris_set_callbacks (callback); return sd; } - -void -sim_close (SIM_DESC sd, int quitting ATTRIBUTE_UNUSED) -{ - cris_cgen_cpu_close (CPU_CPU_DESC (STATE_CPU (sd, 0))); - sim_module_uninstall (sd); -} SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, - char **argv ATTRIBUTE_UNUSED, - char **envp ATTRIBUTE_UNUSED) + char * const *argv ATTRIBUTE_UNUSED, + char * const *envp ATTRIBUTE_UNUSED) { SIM_CPU *current_cpu = STATE_CPU (sd, 0); SIM_ADDR addr; if (sd != NULL) - addr = interp_start_addr != 0 - ? interp_start_addr : bfd_get_start_address (abfd); + addr = cris_start_address != (SIM_ADDR) -1 + ? cris_start_address + : (interp_start_addr != 0 + ? interp_start_addr + : bfd_get_start_address (abfd)); else addr = 0; sim_pc_set (current_cpu, addr); - /* Other simulators have #if 0:d code that says - STATE_ARGV (sd) = sim_copy_argv (argv); - STATE_ENVP (sd) = sim_copy_argv (envp); - Enabling that gives you not-found link-errors for sim_copy_argv. - FIXME: Do archaeology to find out more. */ + /* Standalone mode (i.e. `run`) will take care of the argv for us in + sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim' + with `gdb`), we need to handle it because the user can change the + argv on the fly via gdb's 'run'. */ + if (STATE_PROG_ARGV (sd) != argv) + { + freeargv (STATE_PROG_ARGV (sd)); + STATE_PROG_ARGV (sd) = dupargv (argv); + } return SIM_RC_OK; } - -void -sim_do_command (SIM_DESC sd, char *cmd) -{ - if (sim_args_command (sd, cmd) != SIM_RC_OK) - sim_io_eprintf (sd, "Unknown command `%s'\n", cmd); -} /* Disassemble an instruction. */