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