Commit | Line | Data |
---|---|---|
f6bcefef | 1 | /* Main simulator entry points specific to the CRIS. |
32d0add0 | 2 | Copyright (C) 2004-2015 Free Software Foundation, Inc. |
f6bcefef HPN |
3 | Contributed by Axis Communications. |
4 | ||
5 | This file is part of the GNU simulators. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
4744ac1b JB |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
f6bcefef HPN |
11 | |
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
4744ac1b JB |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
f6bcefef HPN |
19 | |
20 | /* Based on the fr30 file, mixing in bits from the i960 and pruning of | |
21 | dead code. */ | |
22 | ||
a6ff997c | 23 | #include "config.h" |
f6bcefef HPN |
24 | #include "libiberty.h" |
25 | #include "bfd.h" | |
80e5c09e | 26 | #include "elf-bfd.h" |
f6bcefef HPN |
27 | |
28 | #include "sim-main.h" | |
29 | #ifdef HAVE_STDLIB_H | |
30 | #include <stdlib.h> | |
31 | #endif | |
c10b3605 | 32 | #include <errno.h> |
f6bcefef HPN |
33 | #include "sim-options.h" |
34 | #include "dis-asm.h" | |
35 | ||
36 | /* Apparently the autoconf bits are missing (though HAVE_ENVIRON is used | |
37 | in other dirs; also lacking there). Patch around it for major systems. */ | |
38 | #if defined (HAVE_ENVIRON) || defined (__GLIBC__) | |
39 | extern char **environ; | |
40 | #define GET_ENVIRON() environ | |
41 | #else | |
42 | char *missing_environ[] = { "SHELL=/bin/sh", "PATH=/bin:/usr/bin", NULL }; | |
43 | #define GET_ENVIRON() missing_environ | |
44 | #endif | |
45 | ||
f6bcefef HPN |
46 | /* Used with get_progbounds to find out how much memory is needed for the |
47 | program. We don't want to allocate more, since that could mask | |
48 | invalid memory accesses program bugs. */ | |
49 | struct progbounds { | |
50 | USI startmem; | |
51 | USI endmem; | |
80e5c09e HPN |
52 | USI end_loadmem; |
53 | USI start_nonloadmem; | |
f6bcefef HPN |
54 | }; |
55 | ||
56 | static void free_state (SIM_DESC); | |
80e5c09e | 57 | static void get_progbounds_iterator (bfd *, asection *, void *); |
f6bcefef HPN |
58 | static SIM_RC cris_option_handler (SIM_DESC, sim_cpu *, int, char *, int); |
59 | ||
60 | /* Since we don't build the cgen-opcode table, we use the old | |
61 | disassembler. */ | |
62 | static CGEN_DISASSEMBLER cris_disassemble_insn; | |
63 | ||
64 | /* By default, we set up stack and environment variables like the Linux | |
65 | kernel. */ | |
66 | static char cris_bare_iron = 0; | |
67 | ||
68 | /* Whether 0x9000000xx have simulator-specific meanings. */ | |
aad3b3cb | 69 | char cris_have_900000xxif = 0; |
f6bcefef | 70 | |
c10b3605 HPN |
71 | /* Used to optionally override the default start address of the |
72 | simulation. */ | |
73 | static USI cris_start_address = 0xffffffffu; | |
74 | ||
75 | /* Used to optionally add offsets to the loaded image and its start | |
76 | address. (Not used for the interpreter of dynamically loaded | |
77 | programs or the DSO:s.) */ | |
78 | static int cris_program_offset = 0; | |
79 | ||
466b1d33 HPN |
80 | /* What to do when we face a more or less unknown syscall. */ |
81 | enum cris_unknown_syscall_action_type cris_unknown_syscall_action | |
82 | = CRIS_USYSC_MSG_STOP; | |
83 | ||
f6bcefef HPN |
84 | /* Records simulator descriptor so utilities like cris_dump_regs can be |
85 | called from gdb. */ | |
86 | SIM_DESC current_state; | |
87 | ||
88 | /* CRIS-specific options. */ | |
89 | typedef enum { | |
90 | OPTION_CRIS_STATS = OPTION_START, | |
91 | OPTION_CRIS_TRACE, | |
92 | OPTION_CRIS_NAKED, | |
c10b3605 HPN |
93 | OPTION_CRIS_PROGRAM_OFFSET, |
94 | OPTION_CRIS_STARTADDR, | |
f6bcefef | 95 | OPTION_CRIS_900000XXIF, |
466b1d33 | 96 | OPTION_CRIS_UNKNOWN_SYSCALL |
f6bcefef HPN |
97 | } CRIS_OPTIONS; |
98 | ||
99 | static const OPTION cris_options[] = | |
100 | { | |
101 | { {"cris-cycles", required_argument, NULL, OPTION_CRIS_STATS}, | |
102 | '\0', "basic|unaligned|schedulable|all", | |
103 | "Dump execution statistics", | |
104 | cris_option_handler, NULL }, | |
105 | { {"cris-trace", required_argument, NULL, OPTION_CRIS_TRACE}, | |
106 | '\0', "basic", | |
107 | "Emit trace information while running", | |
108 | cris_option_handler, NULL }, | |
109 | { {"cris-naked", no_argument, NULL, OPTION_CRIS_NAKED}, | |
110 | '\0', NULL, "Don't set up stack and environment", | |
111 | cris_option_handler, NULL }, | |
112 | { {"cris-900000xx", no_argument, NULL, OPTION_CRIS_900000XXIF}, | |
113 | '\0', NULL, "Define addresses at 0x900000xx with simulator semantics", | |
114 | cris_option_handler, NULL }, | |
466b1d33 HPN |
115 | { {"cris-unknown-syscall", required_argument, NULL, |
116 | OPTION_CRIS_UNKNOWN_SYSCALL}, | |
117 | '\0', "stop|enosys|enosys-quiet", "Action at an unknown system call", | |
118 | cris_option_handler, NULL }, | |
c10b3605 HPN |
119 | { {"cris-program-offset", required_argument, NULL, |
120 | OPTION_CRIS_PROGRAM_OFFSET}, | |
121 | '\0', "OFFSET", | |
122 | "Offset image addresses and default start address of a program", | |
123 | cris_option_handler }, | |
124 | { {"cris-start-address", required_argument, NULL, OPTION_CRIS_STARTADDR}, | |
125 | '\0', "ADDRESS", "Set start address", | |
126 | cris_option_handler }, | |
f6bcefef HPN |
127 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL } |
128 | }; | |
129 | \f | |
f6bcefef HPN |
130 | /* Handle CRIS-specific options. */ |
131 | ||
132 | static SIM_RC | |
133 | cris_option_handler (SIM_DESC sd, sim_cpu *cpu ATTRIBUTE_UNUSED, int opt, | |
134 | char *arg, int is_command ATTRIBUTE_UNUSED) | |
135 | { | |
136 | /* The options are CRIS-specific, but cpu-specific option-handling is | |
137 | broken; required to being with "--cpu0-". We store the flags in an | |
138 | unused field in the global state structure and move the flags over | |
139 | to the module-specific CPU data when we store things in the | |
140 | cpu-specific structure. */ | |
141 | char *tracefp = STATE_TRACE_FLAGS (sd); | |
c10b3605 | 142 | char *chp = arg; |
f6bcefef HPN |
143 | |
144 | switch ((CRIS_OPTIONS) opt) | |
145 | { | |
146 | case OPTION_CRIS_STATS: | |
147 | if (strcmp (arg, "basic") == 0) | |
148 | *tracefp = FLAG_CRIS_MISC_PROFILE_SIMPLE; | |
149 | else if (strcmp (arg, "unaligned") == 0) | |
150 | *tracefp | |
151 | = (FLAG_CRIS_MISC_PROFILE_UNALIGNED | |
152 | | FLAG_CRIS_MISC_PROFILE_SIMPLE); | |
153 | else if (strcmp (arg, "schedulable") == 0) | |
154 | *tracefp | |
155 | = (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE | |
156 | | FLAG_CRIS_MISC_PROFILE_SIMPLE); | |
157 | else if (strcmp (arg, "all") == 0) | |
158 | *tracefp = FLAG_CRIS_MISC_PROFILE_ALL; | |
159 | else | |
160 | { | |
466b1d33 HPN |
161 | /* Beware; the framework does not handle the error case; |
162 | we have to do it ourselves. */ | |
163 | sim_io_eprintf (sd, "Unknown option `--cris-cycles=%s'\n", arg); | |
f6bcefef HPN |
164 | return SIM_RC_FAIL; |
165 | } | |
166 | break; | |
167 | ||
168 | case OPTION_CRIS_TRACE: | |
169 | if (strcmp (arg, "basic") == 0) | |
170 | *tracefp |= FLAG_CRIS_MISC_PROFILE_XSIM_TRACE; | |
171 | else | |
172 | { | |
173 | sim_io_eprintf (sd, "Unknown option `--cris-trace=%s'\n", arg); | |
174 | return SIM_RC_FAIL; | |
175 | } | |
176 | break; | |
177 | ||
178 | case OPTION_CRIS_NAKED: | |
179 | cris_bare_iron = 1; | |
180 | break; | |
181 | ||
182 | case OPTION_CRIS_900000XXIF: | |
183 | cris_have_900000xxif = 1; | |
184 | break; | |
185 | ||
c10b3605 HPN |
186 | case OPTION_CRIS_STARTADDR: |
187 | errno = 0; | |
188 | cris_start_address = (USI) strtoul (chp, &chp, 0); | |
189 | ||
190 | if (errno != 0 || *chp != 0) | |
191 | { | |
192 | sim_io_eprintf (sd, "Invalid option `--cris-start-address=%s'\n", | |
193 | arg); | |
194 | return SIM_RC_FAIL; | |
195 | } | |
196 | break; | |
197 | ||
198 | case OPTION_CRIS_PROGRAM_OFFSET: | |
199 | errno = 0; | |
200 | cris_program_offset = (int) strtol (chp, &chp, 0); | |
201 | ||
202 | if (errno != 0 || *chp != 0) | |
203 | { | |
204 | sim_io_eprintf (sd, "Invalid option `--cris-program-offset=%s'\n", | |
205 | arg); | |
206 | return SIM_RC_FAIL; | |
207 | } | |
208 | break; | |
209 | ||
466b1d33 HPN |
210 | case OPTION_CRIS_UNKNOWN_SYSCALL: |
211 | if (strcmp (arg, "enosys") == 0) | |
212 | cris_unknown_syscall_action = CRIS_USYSC_MSG_ENOSYS; | |
213 | else if (strcmp (arg, "enosys-quiet") == 0) | |
214 | cris_unknown_syscall_action = CRIS_USYSC_QUIET_ENOSYS; | |
215 | else if (strcmp (arg, "stop") == 0) | |
216 | cris_unknown_syscall_action = CRIS_USYSC_MSG_STOP; | |
217 | else | |
218 | { | |
219 | sim_io_eprintf (sd, "Unknown option `--cris-unknown-syscall=%s'\n", | |
220 | arg); | |
221 | return SIM_RC_FAIL; | |
222 | } | |
223 | break; | |
224 | ||
f6bcefef HPN |
225 | default: |
226 | /* We'll actually never get here; the caller handles the error | |
227 | case. */ | |
228 | sim_io_eprintf (sd, "Unknown option `%s'\n", arg); | |
229 | return SIM_RC_FAIL; | |
230 | } | |
231 | ||
232 | /* Imply --profile-model=on. */ | |
233 | return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on"); | |
234 | } | |
235 | ||
80e5c09e HPN |
236 | /* FIXME: Remove these, globalize those in sim-load.c, move elsewhere. */ |
237 | ||
238 | static void | |
239 | xprintf (host_callback *callback, const char *fmt, ...) | |
240 | { | |
241 | va_list ap; | |
242 | ||
243 | va_start (ap, fmt); | |
244 | ||
245 | (*callback->vprintf_filtered) (callback, fmt, ap); | |
246 | ||
247 | va_end (ap); | |
248 | } | |
249 | ||
250 | static void | |
251 | eprintf (host_callback *callback, const char *fmt, ...) | |
252 | { | |
253 | va_list ap; | |
254 | ||
255 | va_start (ap, fmt); | |
256 | ||
257 | (*callback->evprintf_filtered) (callback, fmt, ap); | |
258 | ||
259 | va_end (ap); | |
260 | } | |
261 | ||
262 | /* An ELF-specific simplified ../common/sim-load.c:sim_load_file, | |
263 | using the program headers, not sections, in order to make sure that | |
264 | the program headers themeselves are also loaded. The caller is | |
265 | responsible for asserting that ABFD is an ELF file. */ | |
266 | ||
267 | static bfd_boolean | |
268 | cris_load_elf_file (SIM_DESC sd, struct bfd *abfd, sim_write_fn do_write) | |
269 | { | |
270 | Elf_Internal_Phdr *phdr; | |
271 | int n_hdrs; | |
272 | int i; | |
273 | bfd_boolean verbose = STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG; | |
274 | host_callback *callback = STATE_CALLBACK (sd); | |
275 | ||
276 | phdr = elf_tdata (abfd)->phdr; | |
277 | n_hdrs = elf_elfheader (abfd)->e_phnum; | |
278 | ||
279 | /* We're only interested in PT_LOAD; all necessary information | |
280 | should be covered by that. */ | |
281 | for (i = 0; i < n_hdrs; i++) | |
282 | { | |
283 | bfd_byte *buf; | |
284 | bfd_vma lma = STATE_LOAD_AT_LMA_P (sd) | |
285 | ? phdr[i].p_paddr : phdr[i].p_vaddr; | |
286 | ||
287 | if (phdr[i].p_type != PT_LOAD) | |
288 | continue; | |
289 | ||
290 | buf = xmalloc (phdr[i].p_filesz); | |
291 | ||
292 | if (verbose) | |
293 | xprintf (callback, "Loading segment at 0x%lx, size 0x%lx\n", | |
294 | lma, phdr[i].p_filesz); | |
295 | ||
296 | if (bfd_seek (abfd, phdr[i].p_offset, SEEK_SET) != 0 | |
297 | || (bfd_bread (buf, phdr[i].p_filesz, abfd) != phdr[i].p_filesz)) | |
298 | { | |
299 | eprintf (callback, | |
300 | "%s: could not read segment at 0x%lx, size 0x%lx\n", | |
301 | STATE_MY_NAME (sd), lma, phdr[i].p_filesz); | |
302 | free (buf); | |
303 | return FALSE; | |
304 | } | |
305 | ||
306 | if (do_write (sd, lma, buf, phdr[i].p_filesz) != phdr[i].p_filesz) | |
307 | { | |
308 | eprintf (callback, | |
309 | "%s: could not load segment at 0x%lx, size 0x%lx\n", | |
310 | STATE_MY_NAME (sd), lma, phdr[i].p_filesz); | |
311 | free (buf); | |
312 | return FALSE; | |
313 | } | |
314 | ||
315 | free (buf); | |
316 | } | |
317 | ||
318 | return TRUE; | |
319 | } | |
320 | ||
c10b3605 HPN |
321 | /* Helper for sim_load (needed just for ELF files): like sim_write, |
322 | but offset load at cris_program_offset offset. */ | |
323 | ||
324 | static int | |
325 | cris_program_offset_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, | |
326 | int length) | |
327 | { | |
328 | return sim_write (sd, mem + cris_program_offset, buf, length); | |
329 | } | |
330 | ||
80e5c09e HPN |
331 | /* Replacement for ../common/sim-hload.c:sim_load, so we can treat ELF |
332 | files differently. */ | |
333 | ||
334 | SIM_RC | |
b2b255bd | 335 | sim_load (SIM_DESC sd, const char *prog_name, struct bfd *prog_bfd, |
80e5c09e HPN |
336 | int from_tty ATTRIBUTE_UNUSED) |
337 | { | |
338 | bfd *result_bfd; | |
339 | ||
340 | if (bfd_get_flavour (prog_bfd) != bfd_target_elf_flavour) | |
341 | { | |
342 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
343 | if (sim_analyze_program (sd, prog_name, prog_bfd) != SIM_RC_OK) | |
344 | return SIM_RC_FAIL; | |
345 | SIM_ASSERT (STATE_PROG_BFD (sd) != NULL); | |
346 | ||
347 | result_bfd = sim_load_file (sd, STATE_MY_NAME (sd), | |
348 | STATE_CALLBACK (sd), | |
349 | prog_name, | |
350 | STATE_PROG_BFD (sd), | |
351 | STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG, | |
352 | STATE_LOAD_AT_LMA_P (sd), | |
353 | sim_write); | |
354 | if (result_bfd == NULL) | |
355 | { | |
356 | bfd_close (STATE_PROG_BFD (sd)); | |
357 | STATE_PROG_BFD (sd) = NULL; | |
358 | return SIM_RC_FAIL; | |
359 | } | |
360 | return SIM_RC_OK; | |
361 | } | |
362 | ||
c10b3605 | 363 | return cris_load_elf_file (sd, prog_bfd, cris_program_offset_write) |
80e5c09e HPN |
364 | ? SIM_RC_OK : SIM_RC_FAIL; |
365 | } | |
366 | ||
f6bcefef HPN |
367 | /* Cover function of sim_state_free to free the cpu buffers as well. */ |
368 | ||
369 | static void | |
370 | free_state (SIM_DESC sd) | |
371 | { | |
372 | if (STATE_MODULES (sd) != NULL) | |
373 | sim_module_uninstall (sd); | |
374 | sim_cpu_free_all (sd); | |
375 | sim_state_free (sd); | |
376 | } | |
377 | ||
c10b3605 HPN |
378 | /* Helper struct for cris_set_section_offset_iterator. */ |
379 | ||
380 | struct offsetinfo | |
381 | { | |
382 | SIM_DESC sd; | |
383 | int offset; | |
384 | }; | |
385 | ||
386 | /* BFD section iterator to offset the LMA and VMA. */ | |
387 | ||
388 | static void | |
389 | cris_set_section_offset_iterator (bfd *abfd, asection *s, void *vp) | |
390 | { | |
391 | struct offsetinfo *p = (struct offsetinfo *) vp; | |
392 | SIM_DESC sd = p->sd; | |
393 | int offset = p->offset; | |
394 | ||
395 | if ((bfd_get_section_flags (abfd, s) & SEC_ALLOC)) | |
396 | { | |
397 | bfd_vma vma = bfd_get_section_vma (abfd, s); | |
398 | ||
399 | bfd_set_section_vma (abfd, s, vma + offset); | |
400 | } | |
401 | ||
402 | /* This seems clumsy and inaccurate, but let's stick to doing it the | |
403 | same way as sim_analyze_program for consistency. */ | |
404 | if (strcmp (bfd_get_section_name (abfd, s), ".text") == 0) | |
405 | STATE_TEXT_START (sd) = bfd_get_section_vma (abfd, s); | |
406 | } | |
407 | ||
408 | /* Adjust the start-address, LMA and VMA of a SD. Must be called | |
409 | after sim_analyze_program. */ | |
410 | ||
411 | static void | |
412 | cris_offset_sections (SIM_DESC sd, int offset) | |
413 | { | |
414 | bfd_boolean ret; | |
415 | struct bfd *abfd = STATE_PROG_BFD (sd); | |
416 | asection *text; | |
417 | struct offsetinfo oi; | |
418 | ||
419 | /* Only happens for usage error. */ | |
420 | if (abfd == NULL) | |
421 | return; | |
422 | ||
423 | oi.sd = sd; | |
424 | oi.offset = offset; | |
425 | ||
426 | bfd_map_over_sections (abfd, cris_set_section_offset_iterator, &oi); | |
427 | ret = bfd_set_start_address (abfd, bfd_get_start_address (abfd) + offset); | |
428 | ||
429 | STATE_START_ADDR (sd) = bfd_get_start_address (abfd); | |
430 | } | |
431 | ||
80e5c09e HPN |
432 | /* BFD section iterator to find the highest and lowest allocated and |
433 | non-allocated section addresses (plus one). */ | |
f6bcefef | 434 | |
80e5c09e HPN |
435 | static void |
436 | get_progbounds_iterator (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *vp) | |
f6bcefef HPN |
437 | { |
438 | struct progbounds *pbp = (struct progbounds *) vp; | |
439 | ||
440 | if ((bfd_get_section_flags (abfd, s) & SEC_ALLOC)) | |
441 | { | |
442 | bfd_size_type sec_size = bfd_get_section_size (s); | |
443 | bfd_size_type sec_start = bfd_get_section_vma (abfd, s); | |
444 | bfd_size_type sec_end = sec_start + sec_size; | |
445 | ||
446 | if (sec_end > pbp->endmem) | |
447 | pbp->endmem = sec_end; | |
448 | ||
449 | if (sec_start < pbp->startmem) | |
450 | pbp->startmem = sec_start; | |
80e5c09e HPN |
451 | |
452 | if ((bfd_get_section_flags (abfd, s) & SEC_LOAD)) | |
453 | { | |
454 | if (sec_end > pbp->end_loadmem) | |
455 | pbp->end_loadmem = sec_end; | |
456 | } | |
457 | else if (sec_start < pbp->start_nonloadmem) | |
458 | pbp->start_nonloadmem = sec_start; | |
459 | } | |
460 | } | |
461 | ||
462 | /* Get the program boundaries. Because not everything is covered by | |
463 | sections in ELF, notably the program headers, we use the program | |
464 | headers instead. */ | |
465 | ||
466 | static void | |
467 | cris_get_progbounds (struct bfd *abfd, struct progbounds *pbp) | |
468 | { | |
469 | Elf_Internal_Phdr *phdr; | |
470 | int n_hdrs; | |
471 | int i; | |
472 | ||
473 | pbp->startmem = 0xffffffff; | |
474 | pbp->endmem = 0; | |
475 | pbp->end_loadmem = 0; | |
476 | pbp->start_nonloadmem = 0xffffffff; | |
477 | ||
478 | /* In case we're ever used for something other than ELF, use the | |
479 | generic method. */ | |
480 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
481 | { | |
482 | bfd_map_over_sections (abfd, get_progbounds_iterator, pbp); | |
483 | return; | |
484 | } | |
485 | ||
486 | phdr = elf_tdata (abfd)->phdr; | |
487 | n_hdrs = elf_elfheader (abfd)->e_phnum; | |
488 | ||
489 | /* We're only interested in PT_LOAD; all necessary information | |
490 | should be covered by that. */ | |
491 | for (i = 0; i < n_hdrs; i++) | |
492 | { | |
493 | if (phdr[i].p_type != PT_LOAD) | |
494 | continue; | |
495 | ||
496 | if (phdr[i].p_paddr < pbp->startmem) | |
497 | pbp->startmem = phdr[i].p_paddr; | |
498 | ||
499 | if (phdr[i].p_paddr + phdr[i].p_memsz > pbp->endmem) | |
500 | pbp->endmem = phdr[i].p_paddr + phdr[i].p_memsz; | |
501 | ||
502 | if (phdr[i].p_paddr + phdr[i].p_filesz > pbp->end_loadmem) | |
503 | pbp->end_loadmem = phdr[i].p_paddr + phdr[i].p_filesz; | |
504 | ||
505 | if (phdr[i].p_memsz > phdr[i].p_filesz | |
506 | && phdr[i].p_paddr + phdr[i].p_filesz < pbp->start_nonloadmem) | |
507 | pbp->start_nonloadmem = phdr[i].p_paddr + phdr[i].p_filesz; | |
508 | } | |
509 | } | |
510 | ||
511 | /* Parameter communication by static variables, hmm... Oh well, for | |
512 | simplicity. */ | |
513 | static bfd_vma exec_load_addr; | |
514 | static bfd_vma interp_load_addr; | |
515 | static bfd_vma interp_start_addr; | |
516 | ||
517 | /* Supposed to mimic Linux' "NEW_AUX_ENT (AT_PHDR, load_addr + exec->e_phoff)". */ | |
518 | ||
519 | static USI | |
520 | aux_ent_phdr (struct bfd *ebfd) | |
521 | { | |
522 | return elf_elfheader (ebfd)->e_phoff + exec_load_addr; | |
523 | } | |
524 | ||
525 | /* We just pass on the header info; we don't have our own idea of the | |
526 | program header entry size. */ | |
527 | ||
528 | static USI | |
529 | aux_ent_phent (struct bfd *ebfd) | |
530 | { | |
531 | return elf_elfheader (ebfd)->e_phentsize; | |
532 | } | |
533 | ||
534 | /* Like "NEW_AUX_ENT(AT_PHNUM, exec->e_phnum)". */ | |
535 | ||
536 | static USI | |
537 | aux_ent_phnum (struct bfd *ebfd) | |
538 | { | |
539 | return elf_elfheader (ebfd)->e_phnum; | |
540 | } | |
541 | ||
542 | /* Like "NEW_AUX_ENT(AT_BASE, interp_load_addr)". */ | |
543 | ||
544 | static USI | |
545 | aux_ent_base (struct bfd *ebfd) | |
546 | { | |
547 | return interp_load_addr; | |
548 | } | |
549 | ||
550 | /* Like "NEW_AUX_ENT(AT_ENTRY, exec->e_entry)". */ | |
551 | ||
552 | static USI | |
553 | aux_ent_entry (struct bfd *ebfd) | |
554 | { | |
555 | ASSERT (elf_elfheader (ebfd)->e_entry == bfd_get_start_address (ebfd)); | |
556 | return elf_elfheader (ebfd)->e_entry; | |
557 | } | |
558 | ||
559 | /* Helper for cris_handle_interpreter: like sim_write, but load at | |
560 | interp_load_addr offset. */ | |
561 | ||
562 | static int | |
563 | cris_write_interp (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length) | |
564 | { | |
565 | return sim_write (sd, mem + interp_load_addr, buf, length); | |
566 | } | |
567 | ||
568 | /* Cater to the presence of an interpreter: load it and set | |
569 | interp_start_addr. Return FALSE if there was an error, TRUE if | |
570 | everything went fine, including an interpreter being absent and | |
571 | the program being in a non-ELF format. */ | |
572 | ||
573 | static bfd_boolean | |
574 | cris_handle_interpreter (SIM_DESC sd, struct bfd *abfd) | |
575 | { | |
576 | int i, n_hdrs; | |
577 | bfd_vma phaddr; | |
578 | bfd_byte buf[4]; | |
579 | char *interp = NULL; | |
580 | struct bfd *ibfd; | |
581 | bfd_boolean ok = FALSE; | |
582 | Elf_Internal_Phdr *phdr; | |
583 | ||
584 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
585 | return TRUE; | |
586 | ||
587 | phdr = elf_tdata (abfd)->phdr; | |
588 | n_hdrs = aux_ent_phnum (abfd); | |
589 | ||
590 | /* Check the program headers for presence of an interpreter. */ | |
591 | for (i = 0; i < n_hdrs; i++) | |
592 | { | |
593 | int interplen; | |
594 | bfd_size_type interpsiz, interp_filesiz; | |
595 | struct progbounds interp_bounds; | |
596 | ||
597 | if (phdr[i].p_type != PT_INTERP) | |
598 | continue; | |
599 | ||
600 | /* Get the name of the interpreter, prepended with the sysroot | |
601 | (empty if absent). */ | |
602 | interplen = phdr[i].p_filesz; | |
603 | interp = xmalloc (interplen + strlen (simulator_sysroot)); | |
604 | strcpy (interp, simulator_sysroot); | |
605 | ||
606 | /* Read in the name. */ | |
607 | if (bfd_seek (abfd, phdr[i].p_offset, SEEK_SET) != 0 | |
608 | || (bfd_bread (interp + strlen (simulator_sysroot), interplen, abfd) | |
609 | != interplen)) | |
610 | goto interpname_failed; | |
611 | ||
612 | /* Like Linux, require the string to be 0-terminated. */ | |
613 | if (interp[interplen + strlen (simulator_sysroot) - 1] != 0) | |
614 | goto interpname_failed; | |
615 | ||
616 | /* Inspect the interpreter. */ | |
617 | ibfd = bfd_openr (interp, STATE_TARGET (sd)); | |
618 | if (ibfd == NULL) | |
619 | goto interpname_failed; | |
620 | ||
c10b3605 | 621 | /* The interpreter is at least something readable to BFD; make |
80e5c09e HPN |
622 | sure it's an ELF non-archive file. */ |
623 | if (!bfd_check_format (ibfd, bfd_object) | |
624 | || bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
625 | goto interp_failed; | |
626 | ||
627 | /* Check the layout of the interpreter. */ | |
628 | cris_get_progbounds (ibfd, &interp_bounds); | |
629 | ||
630 | /* Round down to pagesize the start page and up the endpage. | |
631 | Don't round the *load and *nonload members. */ | |
632 | interp_bounds.startmem &= ~8191; | |
633 | interp_bounds.endmem = (interp_bounds.endmem + 8191) & ~8191; | |
634 | ||
635 | /* Until we need a more dynamic solution, assume we can put the | |
636 | interpreter at this fixed location. NB: this is not what | |
637 | happens for Linux 2008-12-28, but it could and might and | |
638 | perhaps should. */ | |
639 | interp_load_addr = 0x40000; | |
640 | interpsiz = interp_bounds.endmem - interp_bounds.startmem; | |
641 | interp_filesiz = interp_bounds.end_loadmem - interp_bounds.startmem; | |
642 | ||
643 | /* If we have a non-DSO or interpreter starting at the wrong | |
644 | address, bail. */ | |
645 | if (interp_bounds.startmem != 0 | |
646 | || interpsiz + interp_load_addr >= exec_load_addr) | |
647 | goto interp_failed; | |
648 | ||
649 | /* We don't have the API to get the address of a simulator | |
650 | memory area, so we go via a temporary area. Luckily, the | |
651 | interpreter is supposed to be small, less than 0x40000 | |
652 | bytes. */ | |
653 | sim_do_commandf (sd, "memory region 0x%lx,0x%lx", | |
654 | interp_load_addr, interpsiz); | |
655 | ||
656 | /* Now that memory for the interpreter is defined, load it. */ | |
657 | if (!cris_load_elf_file (sd, ibfd, cris_write_interp)) | |
658 | goto interp_failed; | |
659 | ||
660 | /* It's no use setting STATE_START_ADDR, because it gets | |
661 | overwritten by a sim_analyze_program call in sim_load. Let's | |
662 | just store it locally. */ | |
663 | interp_start_addr | |
664 | = (bfd_get_start_address (ibfd) | |
665 | - interp_bounds.startmem + interp_load_addr); | |
666 | ||
667 | /* Linux cares only about the first PT_INTERP, so let's ignore | |
668 | the rest. */ | |
669 | goto all_done; | |
f6bcefef | 670 | } |
80e5c09e HPN |
671 | |
672 | /* Register R10 should hold 0 at static start (no finifunc), but | |
673 | that's the default, so don't bother. */ | |
674 | return TRUE; | |
675 | ||
676 | all_done: | |
677 | ok = TRUE; | |
678 | ||
679 | interp_failed: | |
680 | bfd_close (ibfd); | |
681 | ||
682 | interpname_failed: | |
683 | if (!ok) | |
684 | sim_io_eprintf (sd, | |
685 | "%s: could not load ELF interpreter `%s' for program `%s'\n", | |
686 | STATE_MY_NAME (sd), | |
687 | interp == NULL ? "(what's-its-name)" : interp, | |
688 | bfd_get_filename (abfd)); | |
689 | free (interp); | |
690 | return ok; | |
f6bcefef HPN |
691 | } |
692 | ||
693 | /* Create an instance of the simulator. */ | |
694 | ||
695 | SIM_DESC | |
696 | sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd, | |
697 | char **argv) | |
698 | { | |
699 | char c; | |
700 | int i; | |
701 | USI startmem = 0; | |
702 | USI endmem = CRIS_DEFAULT_MEM_SIZE; | |
703 | USI endbrk = endmem; | |
704 | USI stack_low = 0; | |
705 | SIM_DESC sd = sim_state_alloc (kind, callback); | |
706 | ||
80e5c09e HPN |
707 | static const struct auxv_entries_s |
708 | { | |
709 | bfd_byte id; | |
710 | USI (*efn) (struct bfd *ebfd); | |
711 | USI val; | |
712 | } auxv_entries[] = | |
713 | { | |
a3d4b83b HPN |
714 | #define AUX_ENT(a, b) {a, NULL, b} |
715 | #define AUX_ENTF(a, f) {a, f, 0} | |
80e5c09e HPN |
716 | AUX_ENT (AT_HWCAP, 0), |
717 | AUX_ENT (AT_PAGESZ, 8192), | |
718 | AUX_ENT (AT_CLKTCK, 100), | |
a3d4b83b HPN |
719 | AUX_ENTF (AT_PHDR, aux_ent_phdr), |
720 | AUX_ENTF (AT_PHENT, aux_ent_phent), | |
721 | AUX_ENTF (AT_PHNUM, aux_ent_phnum), | |
722 | AUX_ENTF (AT_BASE, aux_ent_base), | |
80e5c09e | 723 | AUX_ENT (AT_FLAGS, 0), |
a3d4b83b | 724 | AUX_ENTF (AT_ENTRY, aux_ent_entry), |
80e5c09e HPN |
725 | |
726 | /* Or is root better? Maybe have it settable? */ | |
727 | AUX_ENT (AT_UID, 500), | |
728 | AUX_ENT (AT_EUID, 500), | |
729 | AUX_ENT (AT_GID, 500), | |
730 | AUX_ENT (AT_EGID, 500), | |
a3d4b83b | 731 | AUX_ENT (AT_SECURE, 0), |
80e5c09e HPN |
732 | AUX_ENT (AT_NULL, 0) |
733 | }; | |
734 | ||
f6bcefef HPN |
735 | /* Can't initialize to "" below. It's either a GCC bug in old |
736 | releases (up to and including 2.95.3 (.4 in debian) or a bug in the | |
737 | standard ;-) that the rest of the elements won't be initialized. */ | |
738 | bfd_byte sp_init[4] = {0, 0, 0, 0}; | |
739 | ||
740 | /* The cpu data is kept in a separately allocated chunk of memory. */ | |
741 | if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK) | |
742 | { | |
743 | free_state (sd); | |
744 | return 0; | |
745 | } | |
746 | ||
747 | if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK) | |
748 | { | |
749 | free_state (sd); | |
750 | return 0; | |
751 | } | |
752 | ||
9db2b719 MF |
753 | /* Add the CRIS-specific option list to the simulator. */ |
754 | if (sim_add_option_table (sd, NULL, cris_options) != SIM_RC_OK) | |
755 | { | |
756 | free_state (sd); | |
757 | return 0; | |
758 | } | |
759 | ||
f6bcefef HPN |
760 | /* getopt will print the error message so we just have to exit if this fails. |
761 | FIXME: Hmmm... in the case of gdb we need getopt to call | |
762 | print_filtered. */ | |
763 | if (sim_parse_args (sd, argv) != SIM_RC_OK) | |
764 | { | |
765 | free_state (sd); | |
766 | return 0; | |
767 | } | |
768 | ||
769 | /* If we have a binary program, endianness-setting would not be taken | |
770 | from elsewhere unfortunately, so set it here. At the time of this | |
771 | writing, it isn't used until sim_config, but that might change so | |
772 | set it here before memory is defined or touched. */ | |
773 | current_target_byte_order = LITTLE_ENDIAN; | |
774 | ||
775 | /* check for/establish the reference program image */ | |
776 | if (sim_analyze_program (sd, | |
777 | (STATE_PROG_ARGV (sd) != NULL | |
778 | ? *STATE_PROG_ARGV (sd) | |
779 | : NULL), | |
780 | abfd) != SIM_RC_OK) | |
781 | { | |
a0956358 HPN |
782 | /* When there's an error, sim_analyze_program has already output |
783 | a message. Let's just clarify it, as "not an object file" | |
784 | perhaps doesn't ring a bell. */ | |
785 | sim_io_eprintf (sd, "(not a CRIS program)\n"); | |
80e5c09e HPN |
786 | free_state (sd); |
787 | return 0; | |
788 | } | |
789 | ||
790 | /* We might get called with the caller expecting us to get hold of | |
791 | the bfd for ourselves, which would happen at the | |
792 | sim_analyze_program call above. */ | |
793 | if (abfd == NULL) | |
794 | abfd = STATE_PROG_BFD (sd); | |
795 | ||
c10b3605 HPN |
796 | /* Adjust the addresses of the program at this point. Unfortunately |
797 | this does not affect ELF program headers, so we have to handle | |
798 | that separately. */ | |
799 | cris_offset_sections (sd, cris_program_offset); | |
800 | ||
801 | if (abfd != NULL && bfd_get_arch (abfd) == bfd_arch_unknown) | |
80e5c09e HPN |
802 | { |
803 | if (STATE_PROG_ARGV (sd) != NULL) | |
a0956358 | 804 | sim_io_eprintf (sd, "%s: `%s' is not a CRIS program\n", |
80e5c09e HPN |
805 | STATE_MY_NAME (sd), *STATE_PROG_ARGV (sd)); |
806 | else | |
807 | sim_io_eprintf (sd, "%s: program to be run is not a CRIS program\n", | |
808 | STATE_MY_NAME (sd)); | |
f6bcefef HPN |
809 | free_state (sd); |
810 | return 0; | |
811 | } | |
812 | ||
813 | /* For CRIS simulator-specific use, we need to find out the bounds of | |
814 | the program as well, which is not done by sim_analyze_program | |
815 | above. */ | |
80e5c09e | 816 | if (abfd != NULL) |
f6bcefef HPN |
817 | { |
818 | struct progbounds pb; | |
819 | ||
820 | /* The sections should now be accessible using bfd functions. */ | |
80e5c09e | 821 | cris_get_progbounds (abfd, &pb); |
f6bcefef HPN |
822 | |
823 | /* We align the area that the program uses to page boundaries. */ | |
824 | startmem = pb.startmem & ~8191; | |
825 | endbrk = pb.endmem; | |
826 | endmem = (endbrk + 8191) & ~8191; | |
827 | } | |
828 | ||
829 | /* Find out how much room is needed for the environment and argv, create | |
830 | that memory and fill it. Only do this when there's a program | |
831 | specified. */ | |
80e5c09e | 832 | if (abfd != NULL && !cris_bare_iron) |
f6bcefef | 833 | { |
80e5c09e | 834 | char *name = bfd_get_filename (abfd); |
f6bcefef HPN |
835 | char **my_environ = GET_ENVIRON (); |
836 | /* We use these maps to give the same behavior as the old xsim | |
837 | simulator. */ | |
838 | USI envtop = 0x40000000; | |
839 | USI stacktop = 0x3e000000; | |
840 | USI envstart; | |
841 | int envc; | |
842 | int len = strlen (name) + 1; | |
843 | USI epp, epp0; | |
844 | USI stacklen; | |
845 | int i; | |
846 | char **prog_argv = STATE_PROG_ARGV (sd); | |
847 | int my_argc = 0; | |
848 | /* All CPU:s have the same memory map, apparently. */ | |
849 | SIM_CPU *cpu = STATE_CPU (sd, 0); | |
850 | USI csp; | |
851 | bfd_byte buf[4]; | |
852 | ||
853 | /* Count in the environment as well. */ | |
854 | for (envc = 0; my_environ[envc] != NULL; envc++) | |
855 | len += strlen (my_environ[envc]) + 1; | |
856 | ||
857 | for (i = 0; prog_argv[i] != NULL; my_argc++, i++) | |
858 | len += strlen (prog_argv[i]) + 1; | |
859 | ||
860 | envstart = (envtop - len) & ~8191; | |
861 | ||
862 | /* Create read-only block for the environment strings. */ | |
863 | sim_core_attach (sd, NULL, 0, access_read, 0, | |
864 | envstart, (len + 8191) & ~8191, | |
865 | 0, NULL, NULL); | |
866 | ||
867 | /* This shouldn't happen. */ | |
868 | if (envstart < stacktop) | |
869 | stacktop = envstart - 64 * 8192; | |
870 | ||
871 | csp = stacktop; | |
872 | ||
873 | /* Note that the linux kernel does not correctly compute the storage | |
874 | needs for the static-exe AUX vector. */ | |
80e5c09e HPN |
875 | |
876 | csp -= sizeof (auxv_entries) / sizeof (auxv_entries[0]) * 4 * 2; | |
f6bcefef HPN |
877 | |
878 | csp -= (envc + 1) * 4; | |
879 | csp -= (my_argc + 1) * 4; | |
880 | csp -= 4; | |
881 | ||
882 | /* Write the target representation of the start-up-value for the | |
883 | stack-pointer suitable for register initialization below. */ | |
884 | bfd_putl32 (csp, sp_init); | |
885 | ||
886 | /* If we make this 1M higher; say 8192*1024, we have to take | |
887 | special precautions for pthreads, because pthreads assumes that | |
888 | the memory that low isn't mmapped, and that it can mmap it | |
889 | without fallback in case of failure (and we fail ungracefully | |
890 | long before *that*: the memory isn't accounted for in our mmap | |
891 | list). */ | |
892 | stack_low = (csp - (7168*1024)) & ~8191; | |
893 | ||
894 | stacklen = stacktop - stack_low; | |
895 | ||
896 | /* Tee hee, we have an executable stack. Well, it's necessary to | |
897 | test GCC trampolines... */ | |
898 | sim_core_attach (sd, NULL, 0, access_read_write_exec, 0, | |
899 | stack_low, stacklen, | |
900 | 0, NULL, NULL); | |
901 | ||
902 | epp = epp0 = envstart; | |
903 | ||
904 | /* Can't use sim_core_write_unaligned_4 without everything | |
905 | initialized when tracing, and then these writes would get into | |
906 | the trace. */ | |
907 | #define write_dword(addr, data) \ | |
908 | do \ | |
909 | { \ | |
910 | USI data_ = data; \ | |
911 | USI addr_ = addr; \ | |
912 | bfd_putl32 (data_, buf); \ | |
913 | if (sim_core_write_buffer (sd, cpu, 0, buf, addr_, 4) != 4) \ | |
914 | goto abandon_chip; \ | |
915 | } \ | |
916 | while (0) | |
917 | ||
918 | write_dword (csp, my_argc); | |
919 | csp += 4; | |
920 | ||
921 | for (i = 0; i < my_argc; i++, csp += 4) | |
922 | { | |
923 | size_t strln = strlen (prog_argv[i]) + 1; | |
924 | ||
925 | if (sim_core_write_buffer (sd, cpu, 0, prog_argv[i], epp, strln) | |
926 | != strln) | |
927 | goto abandon_chip; | |
928 | ||
929 | write_dword (csp, envstart + epp - epp0); | |
930 | epp += strln; | |
931 | } | |
932 | ||
933 | write_dword (csp, 0); | |
934 | csp += 4; | |
935 | ||
936 | for (i = 0; i < envc; i++, csp += 4) | |
937 | { | |
938 | unsigned int strln = strlen (my_environ[i]) + 1; | |
939 | ||
940 | if (sim_core_write_buffer (sd, cpu, 0, my_environ[i], epp, strln) | |
941 | != strln) | |
942 | goto abandon_chip; | |
943 | ||
944 | write_dword (csp, envstart + epp - epp0); | |
945 | epp += strln; | |
946 | } | |
947 | ||
948 | write_dword (csp, 0); | |
949 | csp += 4; | |
950 | ||
80e5c09e HPN |
951 | /* The load address of the executable could presumably be |
952 | different than the lowest used memory address, but let's | |
953 | stick to simplicity until needed. And | |
954 | cris_handle_interpreter might change startmem and endmem, so | |
955 | let's set it now. */ | |
956 | exec_load_addr = startmem; | |
f6bcefef | 957 | |
80e5c09e HPN |
958 | if (!cris_handle_interpreter (sd, abfd)) |
959 | goto abandon_chip; | |
f6bcefef | 960 | |
80e5c09e HPN |
961 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
962 | for (i = 0; i < sizeof (auxv_entries) / sizeof (auxv_entries[0]); i++) | |
963 | { | |
964 | write_dword (csp, auxv_entries[i].id); | |
965 | write_dword (csp + 4, | |
966 | auxv_entries[i].efn != NULL | |
967 | ? (*auxv_entries[i].efn) (abfd) | |
968 | : auxv_entries[i].val); | |
969 | csp += 4 + 4; | |
970 | } | |
f6bcefef HPN |
971 | } |
972 | ||
973 | /* Allocate core managed memory if none specified by user. */ | |
974 | if (sim_core_read_buffer (sd, NULL, read_map, &c, startmem, 1) == 0) | |
975 | sim_do_commandf (sd, "memory region 0x%lx,0x%lx", startmem, | |
976 | endmem - startmem); | |
977 | ||
978 | /* Allocate simulator I/O managed memory if none specified by user. */ | |
979 | if (cris_have_900000xxif) | |
34cf5112 | 980 | sim_hw_parse (sd, "/core/%s/reg %#x %i", "cris_900000xx", 0x90000000, 0x100); |
f6bcefef HPN |
981 | |
982 | /* Establish any remaining configuration options. */ | |
983 | if (sim_config (sd) != SIM_RC_OK) | |
984 | { | |
985 | abandon_chip: | |
986 | free_state (sd); | |
987 | return 0; | |
988 | } | |
989 | ||
990 | if (sim_post_argv_init (sd) != SIM_RC_OK) | |
991 | { | |
992 | free_state (sd); | |
993 | return 0; | |
994 | } | |
995 | ||
996 | /* Open a copy of the cpu descriptor table. */ | |
997 | { | |
998 | CGEN_CPU_DESC cd = cris_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name, | |
999 | CGEN_ENDIAN_LITTLE); | |
1000 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
1001 | { | |
1002 | SIM_CPU *cpu = STATE_CPU (sd, i); | |
1003 | CPU_CPU_DESC (cpu) = cd; | |
1004 | CPU_DISASSEMBLER (cpu) = cris_disassemble_insn; | |
1005 | ||
1006 | /* See cris_option_handler for the reason why this is needed. */ | |
1007 | CPU_CRIS_MISC_PROFILE (cpu)->flags = STATE_TRACE_FLAGS (sd)[0]; | |
1008 | ||
1009 | /* Set SP to the stack we allocated above. */ | |
1010 | (* CPU_REG_STORE (cpu)) (cpu, H_GR_SP, (char *) sp_init, 4); | |
1011 | ||
1012 | /* Set the simulator environment data. */ | |
1013 | cpu->highest_mmapped_page = NULL; | |
1014 | cpu->endmem = endmem; | |
1015 | cpu->endbrk = endbrk; | |
1016 | cpu->stack_low = stack_low; | |
1017 | cpu->syscalls = 0; | |
1018 | cpu->m1threads = 0; | |
1019 | cpu->threadno = 0; | |
1020 | cpu->max_threadid = 0; | |
1021 | cpu->thread_data = NULL; | |
1022 | memset (cpu->sighandler, 0, sizeof (cpu->sighandler)); | |
1023 | cpu->make_thread_cpu_data = NULL; | |
1024 | cpu->thread_cpu_data_size = 0; | |
aad3b3cb HPN |
1025 | #if WITH_HW |
1026 | cpu->deliver_interrupt = NULL; | |
1027 | #endif | |
f6bcefef | 1028 | } |
aad3b3cb HPN |
1029 | #if WITH_HW |
1030 | /* Always be cycle-accurate and call before/after functions if | |
1031 | with-hardware. */ | |
1032 | sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on"); | |
1033 | #endif | |
f6bcefef HPN |
1034 | } |
1035 | ||
1036 | /* Initialize various cgen things not done by common framework. | |
1037 | Must be done after cris_cgen_cpu_open. */ | |
1038 | cgen_init (sd); | |
1039 | ||
1040 | /* Store in a global so things like cris_dump_regs can be invoked | |
1041 | from the gdb command line. */ | |
1042 | current_state = sd; | |
1043 | ||
1044 | cris_set_callbacks (callback); | |
1045 | ||
1046 | return sd; | |
1047 | } | |
f6bcefef HPN |
1048 | \f |
1049 | SIM_RC | |
1050 | sim_create_inferior (SIM_DESC sd, struct bfd *abfd, | |
1051 | char **argv ATTRIBUTE_UNUSED, | |
1052 | char **envp ATTRIBUTE_UNUSED) | |
1053 | { | |
1054 | SIM_CPU *current_cpu = STATE_CPU (sd, 0); | |
1055 | SIM_ADDR addr; | |
1056 | ||
80e5c09e | 1057 | if (sd != NULL) |
c10b3605 HPN |
1058 | addr = cris_start_address != (SIM_ADDR) -1 |
1059 | ? cris_start_address | |
1060 | : (interp_start_addr != 0 | |
1061 | ? interp_start_addr | |
1062 | : bfd_get_start_address (abfd)); | |
f6bcefef HPN |
1063 | else |
1064 | addr = 0; | |
1065 | sim_pc_set (current_cpu, addr); | |
1066 | ||
0e967299 MF |
1067 | /* Standalone mode (i.e. `run`) will take care of the argv for us in |
1068 | sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim' | |
1069 | with `gdb`), we need to handle it because the user can change the | |
1070 | argv on the fly via gdb's 'run'. */ | |
1071 | if (STATE_PROG_ARGV (sd) != argv) | |
1072 | { | |
1073 | freeargv (STATE_PROG_ARGV (sd)); | |
1074 | STATE_PROG_ARGV (sd) = dupargv (argv); | |
1075 | } | |
f6bcefef HPN |
1076 | |
1077 | return SIM_RC_OK; | |
1078 | } | |
f6bcefef HPN |
1079 | \f |
1080 | /* Disassemble an instruction. */ | |
1081 | ||
1082 | static void | |
1083 | cris_disassemble_insn (SIM_CPU *cpu, | |
1084 | const CGEN_INSN *insn ATTRIBUTE_UNUSED, | |
1085 | const ARGBUF *abuf ATTRIBUTE_UNUSED, | |
1086 | IADDR pc, char *buf) | |
1087 | { | |
1088 | disassembler_ftype pinsn; | |
1089 | struct disassemble_info disasm_info; | |
1090 | SFILE sfile; | |
1091 | SIM_DESC sd = CPU_STATE (cpu); | |
1092 | ||
1093 | sfile.buffer = sfile.current = buf; | |
1094 | INIT_DISASSEMBLE_INFO (disasm_info, (FILE *) &sfile, | |
1095 | (fprintf_ftype) sim_disasm_sprintf); | |
80e5c09e HPN |
1096 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
1097 | disasm_info.read_memory_func = sim_disasm_read_memory; | |
f6bcefef HPN |
1098 | disasm_info.memory_error_func = sim_disasm_perror_memory; |
1099 | disasm_info.application_data = (PTR) cpu; | |
1100 | pinsn = cris_get_disassembler (STATE_PROG_BFD (sd)); | |
1101 | (*pinsn) (pc, &disasm_info); | |
1102 | } |