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