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