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