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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [MemoryLoad.cpp] - Blame information for rev 64

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

Line No. Rev Author Line
1 51 julius
/* MemoryLoad.cpp -- Program load functions
2
 
3
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
4
   Copyright (C) 2008 Embecosm Limited
5
   Copyright (C) 2009 Julius Baxter, julius@orsoc.se
6
 
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10
 
11
   This program is free software; you can redistribute it and/or modify it
12
   under the terms of the GNU General Public License as published by the Free
13
   Software Foundation; either version 3 of the License, or (at your option)
14
   any later version.
15
 
16
   This program is distributed in the hope that it will be useful, but WITHOUT
17
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19
   more details.
20
 
21
   You should have received a copy of the GNU General Public License along
22
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
 
24
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26
 
27
/* System includes */
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
 
32
#include "MemoryLoad.h"
33
#include "OrpsocMain.h"
34
 
35
 
36
//! Constructor for the ORPSoC memory loader class
37
 
38
//! Initializes various values
39
 
40
//! @param[in] orpsoc  The SystemC Verilated ORPSoC instance
41
//! @param[in] accessor  Accessor class for this Verilated ORPSoC model
42
 
43
MemoryLoad::MemoryLoad
44
(
45
 OrpsocAccess             *_accessor
46
 )
47
{
48
  accessor = _accessor;
49
}       // MemoryAccess ()
50
 
51
 
52
/*---------------------------------------------------------------------------*/
53
/*!Copy a string with null termination
54
 
55
   This function is very similar to strncpy, except it null terminates the
56
   string. A global function also used by the CUC.
57
 
58
   @param[in] dst  The destination string
59
   @param[in] src  The source string
60
   @param[in] n    Number of chars to copy EXCLUDING the null terminator
61
                   (i.e. dst had better have room for n+1 chars)
62
 
63
   @return  A pointer to dst                                                 */
64
/*---------------------------------------------------------------------------*/
65
char *
66
MemoryLoad::strstrip (char       *dst,
67
                      const char *src,
68
                      int         n)
69
{
70
  strncpy (dst, src, n);
71
  *(dst + n) = '\0';
72
 
73
  return  dst;
74
 
75
}       /* strstrip () */
76
 
77
/*---------------------------------------------------------------------------*/
78
/*!Translate logical to physical addresses for the loader
79
 
80
   Used only by the simulator loader to translate logical addresses into
81
   physical.  If loadcode() is called with valid @c virtphy_transl pointer to
82
   a table of translations then translate() performs translation otherwise
83
   physical address is equal to logical.
84
 
85
   Currently NOT used
86
 
87
   @param[in] laddr       Logical address
88
   @param[in] breakpoint  Unused
89
 
90
   @return  The physical address                                             */
91
/*---------------------------------------------------------------------------*/
92
 oraddr_t
93
MemoryLoad::translate (oraddr_t  laddr,
94
           int      *breakpoint)
95
{
96
  /*
97
  int i;
98
 
99
  // No translation (i.e. when loading kernel into simulator)
100
  if (transl_table == 0)
101
    {
102
      return laddr;
103
    }
104
 
105
  // Try to find our translation in the table.
106
  for (i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
107
    {
108
      if ((laddr & ~(PAGE_SIZE - 1)) == eval_direct32 (transl_table + i, 0, 0))
109
        {
110
          // Page modified
111
          set_direct32 (transl_table + i + 8, -2, 0, 0);
112
          PRINTF ("found paddr=%" PRIx32 "\n",
113
                  eval_direct32 (transl_table + i + 4, 0, 0) |
114
                  (laddr & (PAGE_SIZE - 1)));
115
          return  (oraddr_t) eval_direct32 (transl_table + i + 4, 0, 0) |
116
                  (laddr & (oraddr_t) (PAGE_SIZE - 1));
117
        }
118
    }
119
 
120
  // Allocate new phy page for us.
121
  for (i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
122
    {
123
      if (eval_direct32 (transl_table + i + 8, 0, 0) == 0)
124
        {
125
          // VPN
126
          set_direct32 (transl_table + i, laddr & ~(PAGE_SIZE - 1), 0, 0);
127
          // PPN
128
          set_direct32 (transl_table + i + 4, (i / 16) * PAGE_SIZE, 0, 0);
129
          // Page modified
130
          //set_direct32 (transl_table + i + 8, -2, 0, 0);
131
          PRINTF ("newly allocated ppn=%" PRIx32 "\n",
132
                  eval_direct32 (transl_table + i + 4, 0, 0));
133
          PRINTF ("newly allocated .ppn=%" PRIxADDR "\n", transl_table + i + 4);
134
          PRINTF ("newly allocated ofs=%" PRIxADDR "\n",
135
                  (laddr & (PAGE_SIZE - 1)));
136
          PRINTF ("newly allocated paddr=%" PRIx32 "\n",
137
                  eval_direct32 (transl_table + i + 4, 0,
138
                                 0) | (laddr & (PAGE_SIZE - 1)));
139
          return  (oraddr_t) eval_direct32 (transl_table + i + 4, 0, 0) |
140
                  (laddr & (oraddr_t) (PAGE_SIZE - 1));
141
        }
142
    }
143
 
144
  // If we come this far then all phy memory is used and we can't find our
145
     page nor allocate new page.
146
  transl_error = 1;
147
  PRINTF ("can't translate %" PRIxADDR "\n", laddr);
148
  exit (1);
149
 
150
  return  -1;
151
  */
152
  return 0;
153
 
154
}       /* translate() */
155
 
156
#if IMM_STATS
157
 int
158
MemoryLoad::bits (uint32_t val)
159
{
160
  int i = 1;
161
  if (!val)
162
    return 0;
163
  while (val != 0 && (int32_t) val != -1)
164
    {
165
      i++;
166
      val = (int32_t) val >> 1;
167
    }
168
  return i;
169
}
170
 
171
 void
172
MemoryLoad::check_insn (uint32_t insn)
173
{
174
  int insn_index = insn_decode (insn);
175
  struct insn_op_struct *opd = op_start[insn_index];
176
  uint32_t data = 0;
177
  int dis = 0;
178
  const char *name;
179
  if (!insn || insn_index < 0)
180
    return;
181
  name = insn_name (insn_index);
182
  if (strcmp (name, "l.nop") == 0 || strcmp (name, "l.sys") == 0)
183
    return;
184
 
185
  while (1)
186
    {
187
      uint32_t tmp = 0 unsigned int nbits = 0;
188
      while (1)
189
        {
190
          tmp |=
191
            ((insn >> (opd->type & OPTYPE_SHR)) & ((1 << opd->data) - 1)) <<
192
            nbits;
193
          nbits += opd->data;
194
          if (opd->type & OPTYPE_OP)
195
            break;
196
          opd++;
197
        }
198
 
199
      /* Do we have to sign extend? */
200
      if (opd->type & OPTYPE_SIG)
201
        {
202
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
203
          if (tmp & (1 << sbit))
204
            tmp |= 0xFFFFFFFF << sbit;
205
        }
206
      if (opd->type & OPTYPE_DIS)
207
        {
208
          /* We have to read register later.  */
209
          data += tmp;
210
          dis = 1;
211
        }
212
      else
213
        {
214
          if (!(opd->type & OPTYPE_REG) || dis)
215
            {
216
              if (!dis)
217
                data = tmp;
218
              if (strcmp (name, "l.movhi") == 0)
219
                {
220
                  movhi = data << 16;
221
                }
222
              else
223
                {
224
                  data |= movhi;
225
                  //PRINTF ("%08x %s\n", data, name);
226
                  if (!(or32_opcodes[insn_index].flags & OR32_IF_DELAY))
227
                    {
228
                      bcnt[bits (data)][0]++;
229
                      bsum[0]++;
230
                    }
231
                  else
232
                    {
233
                      if (strcmp (name, "l.bf") == 0
234
                          || strcmp (name, "l.bnf") == 0)
235
                        {
236
                          bcnt[bits (data)][1]++;
237
                          bsum[1]++;
238
                        }
239
                      else
240
                        {
241
                          bcnt[bits (data)][2]++;
242
                          bsum[2]++;
243
                        }
244
                    }
245
                }
246
            }
247
          data = 0;
248
          dis = 0;
249
        }
250
      if (opd->type & OPTYPE_LAST)
251
        {
252
          return;
253
        }
254
      opd++;
255
    }
256
}
257
#endif
258
 
259
/*---------------------------------------------------------------------------*/
260
/*!Add an instruction to the program
261
 
262
  @note insn must be in big endian format
263
 
264
  @param[in] address     The address to use
265
  @param[in] insn        The instruction to add
266
  @param[in] breakpoint  Not used (it is passed to the translate() function,
267
                         which also does not use it.                         */
268
/*---------------------------------------------------------------------------*/
269
 void
270
MemoryLoad::addprogram (oraddr_t  address,
271
            uint32_t  insn,
272
            int      *breakpoint)
273
{
274
 
275
  // Memory is word addressed, not byte, so /4 the address we get
276
  int vaddr = (int) address/4;  /*(!runtime.sim.filename) ? translate (address, breakpoint) :
277
                                  translate (freemem, breakpoint);
278
                                  -- jb
279
                                */
280
  /* We can't have set_program32 functions since it is not gauranteed that the
281
     section we're loading is aligned on a 4-byte boundry */
282
  /*
283
  set_program8 (vaddr, (insn >> 24) & 0xff);
284
  set_program8 (vaddr + 1, (insn >> 16) & 0xff);
285
  set_program8 (vaddr + 2, (insn >> 8) & 0xff);
286
  set_program8 (vaddr + 3, insn & 0xff);
287
  */
288
  /* Use the whole-word write */
289
  accessor->set_mem(vaddr, insn);
290
  PRINTF("*  addprogram: addr 0x%.8x insn: 0x%.8x (conf: 0x%.8x)\n", vaddr, insn, accessor->get_mem(vaddr));
291
 
292
 
293
#if IMM_STATS
294
  check_insn (insn);
295
#endif
296
 
297
  //if (runtime.sim.filename)
298
  //{
299
  //freemem += insn_len (insn_decode (insn));
300
  //}
301
  freemem += 4;
302
 
303
}       /* addprogram () */
304
 
305
 
306
/*---------------------------------------------------------------------------*/
307
/*!Load big-endian COFF file
308
 
309
   @param[in] filename  File to load
310
   @param[in] sections  Number of sections in file                           */
311
/*---------------------------------------------------------------------------*/
312
 void
313
MemoryLoad::readfile_coff (char  *filename,
314
               short  sections)
315
{
316
  FILE *inputfs;
317
  char inputbuf[4];
318
  uint32_t insn;
319
  int32_t sectsize;
320
  COFF_AOUTHDR coffaouthdr;
321
  struct COFF_scnhdr coffscnhdr;
322
  int len;
323
  int firstthree = 0;
324
  int breakpoint = 0;
325
 
326
  if (!(inputfs = fopen (filename, "r")))
327
    {
328
      perror ("readfile_coff");
329
      exit (1);
330
    }
331
 
332
  if (fseek (inputfs, sizeof (COFF_FILHDR), SEEK_SET) == -1)
333
    {
334
      fclose (inputfs);
335
      perror ("readfile_coff");
336
      exit (1);
337
    }
338
 
339
  if (fread (&coffaouthdr, sizeof (coffaouthdr), 1, inputfs) != 1)
340
    {
341
      fclose (inputfs);
342
      perror ("readfile_coff");
343
      exit (1);
344
    }
345
 
346
  while (sections--)
347
    {
348
      uint32_t scnhdr_pos =
349
        sizeof (COFF_FILHDR) + sizeof (coffaouthdr) +
350
        sizeof (struct COFF_scnhdr) * firstthree;
351
      if (fseek (inputfs, scnhdr_pos, SEEK_SET) == -1)
352
        {
353
          fclose (inputfs);
354
          perror ("readfile_coff");
355
          exit (1);
356
        }
357
      if (fread (&coffscnhdr, sizeof (struct COFF_scnhdr), 1, inputfs) != 1)
358
        {
359
          fclose (inputfs);
360
          perror ("readfile_coff");
361
          exit (1);
362
        }
363
      PRINTF ("Section: %s,", coffscnhdr.s_name);
364
      PRINTF (" paddr: 0x%.8lx,", COFF_LONG_H (coffscnhdr.s_paddr));
365
      PRINTF (" vaddr: 0x%.8lx,", COFF_LONG_H (coffscnhdr.s_vaddr));
366
      PRINTF (" size: 0x%.8lx,", COFF_LONG_H (coffscnhdr.s_size));
367
      PRINTF (" scnptr: 0x%.8lx\n", COFF_LONG_H (coffscnhdr.s_scnptr));
368
 
369
      sectsize = COFF_LONG_H (coffscnhdr.s_size);
370
      ++firstthree;
371
 
372
      /* loading section */
373
      freemem = COFF_LONG_H (coffscnhdr.s_paddr);
374
      if (fseek (inputfs, COFF_LONG_H (coffscnhdr.s_scnptr), SEEK_SET) == -1)
375
        {
376
          fclose (inputfs);
377
          perror ("readfile_coff");
378
          exit (1);
379
        }
380
      while (sectsize > 0
381
             && (len = fread (&inputbuf, sizeof (inputbuf), 1, inputfs)))
382
        {
383
          insn = COFF_LONG_H (inputbuf);
384
          //len = insn_len (insn_decode (insn));
385
          len = 4;
386
          if (len == 2)
387
            {
388
              fseek (inputfs, -2, SEEK_CUR);
389
            }
390
 
391
          addprogram (freemem, insn, &breakpoint);
392
          sectsize -= len;
393
        }
394
    }
395
  if (firstthree < 3)
396
    {
397
      PRINTF ("One or more missing sections. At least");
398
      PRINTF (" three sections expected (.text, .data, .bss).\n");
399
      exit (1);
400
    }
401
  if (firstthree > 3)
402
    {
403
      PRINTF ("Warning: one or more extra sections. These");
404
      PRINTF (" sections were handled as .data sections.\n");
405
    }
406
 
407
  fclose (inputfs);
408
  PRINTF ("Finished loading COFF.\n");
409
  return;
410
 
411
}       /* readfile_coff () */
412
 
413
 
414
/*---------------------------------------------------------------------------*/
415
/*!Load symbols from big-endian COFF file
416
 
417
   @param[in] filename  File to load
418
   @param[in] symptr    Symbol pointer value
419
   @param[in] syms      Symbols value                                        */
420
/*---------------------------------------------------------------------------*/
421
 
422
 void
423
MemoryLoad::readsyms_coff (char *filename, uint32_t symptr, uint32_t syms)
424
{
425
  FILE *inputfs;
426
  struct COFF_syment coffsymhdr;
427
  int count = 0;
428
  uint32_t nsyms = syms;
429
  if (!(inputfs = fopen (filename, "r")))
430
    {
431
      perror ("readsyms_coff");
432
      exit (1);
433
    }
434
 
435
  if (fseek (inputfs, symptr, SEEK_SET) == -1)
436
    {
437
      fclose (inputfs);
438
      perror ("readsyms_coff");
439
      exit (1);
440
    }
441
 
442
  while (syms--)
443
    {
444
      int i, n;
445
      if (fread (&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1)
446
        {
447
          fclose (inputfs);
448
          perror ("readsyms_coff");
449
          exit (1);
450
        }
451
 
452
      n = (unsigned char) coffsymhdr.e_numaux[0];
453
 
454
      /* check whether this symbol belongs to a section and is external
455
         symbol; ignore all others */
456
      if (COFF_SHORT_H (coffsymhdr.e_scnum) >= 0
457
          && coffsymhdr.e_sclass[0] == C_EXT)
458
        {
459
          if (*((uint32_t *) coffsymhdr.e.e.e_zeroes))
460
            {
461
              if (strlen (coffsymhdr.e.e_name)
462
                  && strlen (coffsymhdr.e.e_name) < 9)
463
                add_label (COFF_LONG_H (coffsymhdr.e_value),
464
                           coffsymhdr.e.e_name);
465
            }
466
          else
467
            {
468
              uint32_t fpos = ftell (inputfs);
469
 
470
              if (fseek
471
                  (inputfs,
472
                   symptr + nsyms * COFF_SYMESZ +
473
                   COFF_LONG_H (coffsymhdr.e.e.e_offset), SEEK_SET) == 0)
474
                {
475
                  char tmp[33], *s = &tmp[0];
476
                  while (s != &tmp[32])
477
                    if ((*(s++) = fgetc (inputfs)) == 0)
478
                      break;
479
                  tmp[32] = 0;
480
                  add_label (COFF_LONG_H (coffsymhdr.e_value), &tmp[0]);
481
                }
482
              fseek (inputfs, fpos, SEEK_SET);
483
            }
484
        }
485
 
486
      for (i = 0; i < n; i++)
487
        if (fread (&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1)
488
          {
489
            fclose (inputfs);
490
            perror ("readsyms_coff3");
491
            exit (1);
492
          }
493
      syms -= n;
494
      count += n;
495
    }
496
 
497
  fclose (inputfs);
498
  PRINTF ("Finished loading symbols.\n");
499
  return;
500
}
501
 
502
/*---------------------------------------------------------------------------*/
503
/*!Read an ELF file
504
 
505
   @param[in] filename  File to load                                         */
506
/*---------------------------------------------------------------------------*/
507
 void
508
MemoryLoad::readfile_elf (char *filename)
509
{
510
 
511
  FILE *inputfs;
512
  struct elf32_hdr elfhdr;
513
  struct elf32_phdr *elf_phdata = NULL;
514
  struct elf32_shdr *elf_spnt, *elf_shdata;
515
  struct elf32_sym *sym_tbl = (struct elf32_sym *) 0;
516
  uint32_t syms = 0;
517
  char *str_tbl = (char *) 0;
518
  char *s_str = (char *) 0;
519
  int breakpoint = 0;
520
  uint32_t inputbuf;
521
  uint32_t padd;
522
  uint32_t insn;
523
  int i, j, sectsize, len;
524
 
525
  if (!(inputfs = fopen (filename, "r")))
526
    {
527
      perror ("readfile_elf");
528
      exit (1);
529
    }
530
 
531
  if (fread (&elfhdr, sizeof (elfhdr), 1, inputfs) != 1)
532
    {
533
      perror ("readfile_elf");
534
      exit (1);
535
    }
536
 
537
  if ((elf_shdata =
538
       (struct elf32_shdr *) malloc (ELF_SHORT_H (elfhdr.e_shentsize) *
539
                                     ELF_SHORT_H (elfhdr.e_shnum))) == NULL)
540
    {
541
      perror ("readfile_elf");
542
      exit (1);
543
    }
544
 
545
  if (fseek (inputfs, ELF_LONG_H (elfhdr.e_shoff), SEEK_SET) != 0)
546
    {
547
      perror ("readfile_elf");
548
      exit (1);
549
    }
550
 
551
  if (fread
552
      (elf_shdata,
553
       ELF_SHORT_H (elfhdr.e_shentsize) * ELF_SHORT_H (elfhdr.e_shnum), 1,
554
       inputfs) != 1)
555
    {
556
      perror ("readfile_elf");
557
      exit (1);
558
    }
559
 
560
  if (ELF_LONG_H (elfhdr.e_phoff))
561
    {
562
      if ((elf_phdata =
563
           (struct elf32_phdr *) malloc (ELF_SHORT_H (elfhdr.e_phnum) *
564
                                         ELF_SHORT_H (elfhdr.e_phentsize))) ==
565
          NULL)
566
        {
567
          perror ("readfile_elf");
568
          exit (1);
569
        }
570
 
571
      if (fseek (inputfs, ELF_LONG_H (elfhdr.e_phoff), SEEK_SET) != 0)
572
        {
573
          perror ("readfile_elf");
574
          exit (1);
575
        }
576
 
577
      if (fread
578
          (elf_phdata,
579
           ELF_SHORT_H (elfhdr.e_phnum) * ELF_SHORT_H (elfhdr.e_phentsize),
580
           1, inputfs) != 1)
581
        {
582
          perror ("readfile_elf");
583
          exit (1);
584
        }
585
    }
586
 
587
  for (i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H (elfhdr.e_shnum);
588
       i++, elf_spnt++)
589
    {
590
 
591
      if (ELF_LONG_H (elf_spnt->sh_type) == SHT_STRTAB)
592
        {
593
          if (NULL != str_tbl)
594
            {
595
              free (str_tbl);
596
            }
597
 
598
          if ((str_tbl =
599
               (char *) malloc (ELF_LONG_H (elf_spnt->sh_size))) == NULL)
600
            {
601
              perror ("readfile_elf");
602
              exit (1);
603
            }
604
 
605
          if (fseek (inputfs, ELF_LONG_H (elf_spnt->sh_offset), SEEK_SET) !=
606
              0)
607
            {
608
              perror ("readfile_elf");
609
              exit (1);
610
            }
611
 
612
          if (fread (str_tbl, ELF_LONG_H (elf_spnt->sh_size), 1, inputfs) !=
613
              1)
614
            {
615
              perror ("readfile_elf");
616
              exit (1);
617
            }
618
        }
619
      else if (ELF_LONG_H (elf_spnt->sh_type) == SHT_SYMTAB)
620
        {
621
 
622
          if (NULL != sym_tbl)
623
            {
624
              free (sym_tbl);
625
            }
626
 
627
          if ((sym_tbl =
628
               (struct elf32_sym *) malloc (ELF_LONG_H (elf_spnt->sh_size)))
629
              == NULL)
630
            {
631
              perror ("readfile_elf");
632
              exit (1);
633
            }
634
 
635
          if (fseek (inputfs, ELF_LONG_H (elf_spnt->sh_offset), SEEK_SET) !=
636
              0)
637
            {
638
              perror ("readfile_elf");
639
              exit (1);
640
            }
641
 
642
          if (fread (sym_tbl, ELF_LONG_H (elf_spnt->sh_size), 1, inputfs) !=
643
              1)
644
            {
645
              perror ("readfile_elf");
646
              exit (1);
647
            }
648
 
649
          syms =
650
            ELF_LONG_H (elf_spnt->sh_size) /
651
            ELF_LONG_H (elf_spnt->sh_entsize);
652
        }
653
    }
654
 
655
  if (ELF_SHORT_H (elfhdr.e_shstrndx) != SHN_UNDEF)
656
    {
657
      elf_spnt = &elf_shdata[ELF_SHORT_H (elfhdr.e_shstrndx)];
658
 
659
      if ((s_str = (char *) malloc (ELF_LONG_H (elf_spnt->sh_size))) == NULL)
660
        {
661
          perror ("readfile_elf");
662
          exit (1);
663
        }
664
 
665
      if (fseek (inputfs, ELF_LONG_H (elf_spnt->sh_offset), SEEK_SET) != 0)
666
        {
667
          perror ("readfile_elf");
668
          exit (1);
669
        }
670
 
671
      if (fread (s_str, ELF_LONG_H (elf_spnt->sh_size), 1, inputfs) != 1)
672
        {
673
          perror ("readfile_elf");
674
          exit (1);
675
        }
676
    }
677
 
678
 
679
  for (i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H (elfhdr.e_shnum);
680
       i++, elf_spnt++)
681
    {
682
 
683
      if ((ELF_LONG_H (elf_spnt->sh_type) & SHT_PROGBITS)
684
          && (ELF_LONG_H (elf_spnt->sh_flags) & SHF_ALLOC))
685
        {
686
 
687
          padd = ELF_LONG_H (elf_spnt->sh_addr);
688
          for (j = 0; j < ELF_SHORT_H (elfhdr.e_phnum); j++)
689
            {
690
              if (ELF_LONG_H (elf_phdata[j].p_offset) &&
691
                  ELF_LONG_H (elf_phdata[j].p_offset) <=
692
                  ELF_LONG_H (elf_spnt->sh_offset)
693
                  && (ELF_LONG_H (elf_phdata[j].p_offset) +
694
                      ELF_LONG_H (elf_phdata[j].p_memsz)) >
695
                  ELF_LONG_H (elf_spnt->sh_offset))
696
                padd =
697
                  ELF_LONG_H (elf_phdata[j].p_paddr) +
698
                  ELF_LONG_H (elf_spnt->sh_offset) -
699
                  ELF_LONG_H (elf_phdata[j].p_offset);
700
            }
701
 
702
 
703
 
704
          if (ELF_LONG_H (elf_spnt->sh_name) && s_str)
705 63 julius
            //PRINTF ("Section: %s,", &s_str[ELF_LONG_H (elf_spnt->sh_name)]);
706
            printf("* Section: %s,", &s_str[ELF_LONG_H (elf_spnt->sh_name)]);
707 51 julius
          else
708 63 julius
            //PRINTF ("Section: noname,");
709
            printf ("* Section: noname,");
710
          printf ("* vaddr: 0x%.8lx,", ELF_LONG_H (elf_spnt->sh_addr));
711
          printf ("* paddr: 0x%" PRIx32, padd);
712
          printf ("* offset: 0x%.8lx,", ELF_LONG_H (elf_spnt->sh_offset));
713
          printf ("* size: 0x%.8lx\n", ELF_LONG_H (elf_spnt->sh_size));
714 51 julius
 
715
          freemem = padd;
716
          sectsize = ELF_LONG_H (elf_spnt->sh_size);
717
 
718
          if (fseek (inputfs, ELF_LONG_H (elf_spnt->sh_offset), SEEK_SET) !=
719
              0)
720
            {
721
              perror ("readfile_elf");
722
              free (elf_phdata);
723
              exit (1);
724
            }
725
 
726
          while (sectsize > 0
727
                 && (len = fread (&inputbuf, sizeof (inputbuf), 1, inputfs)))
728
            {
729
              insn = ELF_LONG_H (inputbuf);
730
              //PRINTF("* addprogram(%.8x, %.8x, %d)\n", freemem, insn, breakpoint);
731
              addprogram (freemem, insn, &breakpoint);
732
              sectsize -= 4;
733
            }
734
        }
735
    }
736
 
737
  if (str_tbl)
738
    {
739
      i = 0;
740
      while (syms--)
741
        {
742
          if (sym_tbl[i].st_name && sym_tbl[i].st_info
743
              && ELF_SHORT_H (sym_tbl[i].st_shndx) < 0x8000)
744
            {
745
              add_label (ELF_LONG_H (sym_tbl[i].st_value),
746
                         &str_tbl[ELF_LONG_H (sym_tbl[i].st_name)]);
747
            }
748
          i++;
749
        }
750
    }
751
 
752
  if (NULL != str_tbl)
753
    {
754
      free (str_tbl);
755
    }
756
 
757
  if (NULL != sym_tbl)
758
    {
759
      free (sym_tbl);
760
    }
761
 
762
  free (s_str);
763
  free (elf_phdata);
764
  free (elf_shdata);
765
 
766
}
767
 
768
/* Identify file type and call appropriate readfile_X routine. It only
769
handles orX-coff-big executables at the moment. */
770
 
771
void
772
MemoryLoad::identifyfile (char *filename)
773
{
774
  FILE *inputfs;
775
  COFF_FILHDR coffhdr;
776
  struct elf32_hdr elfhdr;
777
 
778
  if (!(inputfs = fopen (filename, "r")))
779
    {
780
      perror (filename);
781
      fflush (stdout);
782
      fflush (stderr);
783
      exit (1);
784
    }
785
 
786
  if (fread (&coffhdr, sizeof (coffhdr), 1, inputfs) == 1)
787
    {
788
      if (COFF_SHORT_H (coffhdr.f_magic) == 0x17a)
789
        {
790
          uint32_t opthdr_size;
791
          PRINTF ("COFF magic: 0x%.4x\n", COFF_SHORT_H (coffhdr.f_magic));
792
          PRINTF ("COFF flags: 0x%.4x\n", COFF_SHORT_H (coffhdr.f_flags));
793
          PRINTF ("COFF symptr: 0x%.8lx\n", COFF_LONG_H (coffhdr.f_symptr));
794
          if ((COFF_SHORT_H (coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC)
795
            {
796
              PRINTF ("This COFF is not an executable.\n");
797
              exit (1);
798
            }
799
          opthdr_size = COFF_SHORT_H (coffhdr.f_opthdr);
800
          if (opthdr_size != sizeof (COFF_AOUTHDR))
801
            {
802
              PRINTF ("COFF optional header is missing or not recognized.\n");
803
              PRINTF ("COFF f_opthdr: 0x%" PRIx32 "\n", opthdr_size);
804
              exit (1);
805
            }
806
          fclose (inputfs);
807
          readfile_coff (filename, COFF_SHORT_H (coffhdr.f_nscns));
808
          readsyms_coff (filename, COFF_LONG_H (coffhdr.f_symptr),
809
                         COFF_LONG_H (coffhdr.f_nsyms));
810
          return;
811
        }
812
      else
813
        {
814
          PRINTF ("Not COFF file format\n");
815
          fseek (inputfs, 0, SEEK_SET);
816
        }
817
    }
818
  if (fread (&elfhdr, sizeof (elfhdr), 1, inputfs) == 1)
819
    {
820
      if (elfhdr.e_ident[0] == 0x7f && elfhdr.e_ident[1] == 0x45
821
          && elfhdr.e_ident[2] == 0x4c && elfhdr.e_ident[3] == 0x46)
822
        {
823
          PRINTF ("ELF type: 0x%.4x\n", ELF_SHORT_H (elfhdr.e_type));
824
          PRINTF ("ELF machine: 0x%.4x\n", ELF_SHORT_H (elfhdr.e_machine));
825
          PRINTF ("ELF version: 0x%.8lx\n", ELF_LONG_H (elfhdr.e_version));
826
          PRINTF ("ELF sec = %d\n", ELF_SHORT_H (elfhdr.e_shnum));
827
          if (ELF_SHORT_H (elfhdr.e_type) != ET_EXEC)
828
            {
829
              PRINTF ("This ELF is not an executable.\n");
830
              exit (1);
831
            }
832
          fclose (inputfs);
833
          readfile_elf (filename);
834
          return;
835
        }
836
      else
837
        {
838
          PRINTF ("Not ELF file format.\n");
839
          fseek (inputfs, 0, SEEK_SET);
840
        }
841
    }
842
 
843
  perror ("identifyfile2");
844
  fclose (inputfs);
845
 
846
  return;
847
}
848
 
849
 
850
/*---------------------------------------------------------------------------*/
851
/*!Load file to memory
852
 
853
   Loads file to memory starting at address startaddr and returns freemem.
854
 
855
   @param[in] filename        File to load
856
   @param[in] startaddr       Start address at which to load
857
   @param[in] virtphy_transl  Virtual to physical transation table if
858
                              required. Only used for microkernel simulation,
859
                              and not used in Ork1sim at present (set to NULL)
860
 
861
   @return  zero on success, negative on failure.                            */
862
/*---------------------------------------------------------------------------*/
863
uint32_t
864
MemoryLoad::loadcode (char *filename, oraddr_t startaddr, oraddr_t virtphy_transl)
865
{
866
  //int breakpoint = 0;
867
 
868
  init_labels (); // jb
869
 
870
  transl_error = 0;
871
  transl_table = virtphy_transl;
872
  freemem      = startaddr;
873
  PRINTF ("*  MemoryLoad::loadcode: filename %s  startaddr=%" PRIxADDR "  virtphy_transl=%"
874
          PRIxADDR "\n", filename, startaddr, virtphy_transl);
875
  identifyfile (filename);
876
 
877
#if IMM_STATS
878
  {
879
    int i = 0, a = 0, b = 0, c = 0;
880
    PRINTF ("index:arith/branch/jump\n");
881
    for (i = 0; i < 33; i++)
882
      PRINTF ("%2i:\t%3.0f%% / %3.0f%%/ %3.0f%%\t%5i / %5i / %5i\n", i,
883
              100. * (a += bcnt[i][0]) / bsum[0], 100. * (b +=
884
                                                          bcnt[i][1]) /
885
              bsum[1], 100. * (c +=
886
                               bcnt[i][2]) / bsum[2], bcnt[i][0],
887
              bcnt[i][1], bcnt[i][2]);
888
    PRINTF ("\nsum %i %i %i\n", bsum[0], bsum[1], bsum[2]);
889
  }
890
#endif
891
 
892
  /*
893
  if (transl_error)
894
    return -1;
895
  else
896
    return translate (freemem, &breakpoint);
897
  */
898
  return (uint32_t) freemem;
899
 
900
}
901
 
902
/* From arch sim labels.c */
903
void
904
MemoryLoad::init_labels ()
905
{
906
  int i;
907
  for (i = 0; i < LABELS_HASH_SIZE; i++)
908
    label_hash[i] = NULL;
909
}
910
 
911
void
912
MemoryLoad::add_label (oraddr_t addr, char *name)
913
{
914
  struct label_entry **tmp;
915
  tmp = &(label_hash[addr % LABELS_HASH_SIZE]);
916
  for (; *tmp; tmp = &((*tmp)->next));
917
  *tmp = (label_entry *) malloc (sizeof (**tmp));
918
  (*tmp)->name = (char *) malloc (strlen (name) + 1);
919
  (*tmp)->addr = addr;
920
  strcpy ((*tmp)->name, name);
921
  (*tmp)->next = NULL;
922
}
923
 
924
struct label_entry *
925
MemoryLoad::get_label (oraddr_t addr)
926
{
927
  struct label_entry *tmp = label_hash[addr % LABELS_HASH_SIZE];
928
  while (tmp)
929
    {
930
      if (tmp->addr == addr)
931
        return tmp;
932
      tmp = tmp->next;
933
    }
934
  return NULL;
935
}
936
 
937
struct label_entry *
938
MemoryLoad::find_label (char *name)
939
{
940
  int i;
941
  for (i = 0; i < LABELS_HASH_SIZE; i++)
942
    {
943
      struct label_entry *tmp = label_hash[i % LABELS_HASH_SIZE];
944
      while (tmp)
945
        {
946
          if (strcmp (tmp->name, name) == 0)
947
            return tmp;
948
          tmp = tmp->next;
949
        }
950
    }
951
  return NULL;
952
}
953
 
954
/* Searches mem array for a particular label and returns label's address.
955
   If label does not exist, returns 0. */
956
oraddr_t
957
MemoryLoad::eval_label (char *name)
958
{
959
  struct label_entry *le;
960
  char *plus;
961
  char *minus;
962
  int positive_offset = 0;
963
  int negative_offset = 0;
964
 
965
  if ((plus = strchr (name, '+')))
966
    {
967
      *plus = '\0';
968
      positive_offset = atoi (++plus);
969
    }
970
 
971
  if ((minus = strchr (name, '-')))
972
    {
973
      *minus = '\0';
974
      negative_offset = atoi (++minus);
975
    }
976
  le = find_label (name);
977
  if (!le)
978
    return 0;
979
 
980
  return le->addr + positive_offset - negative_offset;
981
}

powered by: WebSVN 2.1.0

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