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