OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [sim/] [cris/] [sim-if.c] - Blame information for rev 816

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.