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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_42/] [or1ksim/] [cpu/] [common/] [parse.c] - Blame information for rev 808

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

Line No. Rev Author Line
1 138 markom
/* parce.c -- Architecture independent load
2 2 cvs
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#include <stdio.h>
21
#include <ctype.h>
22
#include <string.h>
23
#include <stdlib.h>
24
 
25
#include "abstract.h"
26 18 lampret
#include "arch.h"
27 67 lampret
#include "dmmu.h"
28 28 lampret
#include "coff.h"
29 808 simons
#include "elf.h"
30 138 markom
#include "debug_unit.h"
31 134 markom
#include "opcode/or32.h"
32 138 markom
#include "parse.h"
33 242 markom
#include "sim-config.h"
34 2 cvs
 
35 221 markom
#define MEMORY_LEN 0x100000000
36 361 markom
#define MAXLINE_LEN 18000
37 2 cvs
 
38 694 markom
/* Whether to do immediate statistics */
39
#define IMM_STATS 0
40
 
41 28 lampret
extern char *disassembled;
42
 
43 2 cvs
/* Unused mem memory marker. It is used when allocating program and data memory
44
   during parsing */
45
unsigned int freemem;
46
 
47 67 lampret
/* Translation table provided by microkernel. Only used if simulating microkernel. */
48
static unsigned long transl_table;
49
 
50
/* Used to signal whether during loading of programs a translation fault occured. */
51
static unsigned long transl_error;
52
 
53
 
54 2 cvs
char *strtoken(char *in, char *out, int which)
55
{
56 361 markom
  char  *super;
57
  char  *sub;
58
  char  *newline;
59
 
60
  super = strdup(in);
61
  sub = strtok(super, " \t");
62
  while (sub && --which)
63
    sub = strtok(NULL, " \t");
64
  if (sub && !which) {
65
    if ((newline = strchr(sub, '\n')))
66
      newline[0] = '\0';
67
    strcpy(out, sub);
68
  } else
69
    out[0] = '\0';
70
  free(super);
71
  if ((newline = strchr(out, '\r')))  /* get rid of CR */
72
    newline[0] = '\0';
73
  return(out);
74 2 cvs
}
75
 
76 304 markom
char *
77
stripwhite (string)
78
     char *string;
79
{
80
  register char *s, *t;
81
 
82
  for (s = string; whitespace (*s); s++)
83
    ;
84
 
85
  if (*s == 0)
86
    return (s);
87
 
88
  t = s + strlen (s) - 1;
89
  while (t > s && whitespace (*t))
90
    t--;
91
  *++t = '\0';
92
 
93
  return s;
94
}
95
 
96
char *
97
dupstr (s)
98
     char *s;
99
{
100
  char *r;
101
 
102 306 markom
  r = (char *)malloc (strlen (s) + 1);
103 304 markom
  strcpy (r, s);
104
  return (r);
105
}
106
 
107 221 markom
/* Used only by the simulator loader to translate logical addresses into physical.
108 67 lampret
   If loadcode() is called with valid virtphy_transl pointer to a table of
109
   translations then translate() performs translation otherwise phy address is
110
   equal to logical. */
111 123 markom
static unsigned int translate(unsigned int laddr,int* breakpoint)
112 67 lampret
{
113 361 markom
  int i;
114
 
115
  /* No translation (i.e. when loading kernel into simulator)
116
/*  printf("transl_table=%x  laddr=%x\n", transl_table, laddr);
117
  printf("laddr=%x\n", laddr);*/
118
  if (transl_table == 0)
119
    return laddr;
120
 
121
  /* Try to find our translation in the table. */
122
  for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
123
    if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i)) {
124
      setsim_mem32(transl_table + i + 8, -2); /* Page modified */
125
      printf("found paddr=%x\n", evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
126
      return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
127
    }
128 67 lampret
 
129 361 markom
  /* Allocate new phy page for us. */
130
  for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
131
    if (evalsim_mem32(transl_table + i + 8) == 0) {
132
      setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
133
      setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
134
      setsim_mem32(transl_table + i + 8, -2); /* Page modified */
135
      printf("newly allocated ppn=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4));
136
      printf("newly allocated .ppn=%x\n", (unsigned long)transl_table + i + 4);
137
      printf("newly allocated ofs=%x\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
138
      printf("newly allocated paddr=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
139
      return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
140
    }
141
  /* If we come this far then all phy memory is used and we can't find our page
142
     nor allocate new page. */
143
  transl_error = 1;
144
 
145
  printf("can't translate\n", laddr);
146
  exit(1);
147
  return -1;
148 67 lampret
}
149
 
150 694 markom
#if IMM_STATS
151 703 markom
int bcnt[33][3] = {0};
152
int bsum[3] = {0};
153
unsigned long movhi = 0;
154 694 markom
 
155
int bits (unsigned long val) {
156
  int i = 1;
157
  if (!val) return 0;
158 703 markom
  while (val != 0 && (signed long)val != -1) {i++; val = (signed long)val >> 1;}
159 694 markom
  return i;
160
}
161
 
162
void check_insn (unsigned long insn) {
163
  int insn_index = insn_decode (insn);
164
  struct insn_op_struct *opd = op_start[insn_index];
165
  unsigned long data = 0;
166
  int dis = 0;
167 703 markom
  const char *name;
168 694 markom
  if (!insn || insn_index < 0) return;
169
  name = insn_name (insn_index);
170 703 markom
  if (strcmp (name, "l.nop") == 0 || strcmp (name, "l.sys") == 0) return;
171 694 markom
 
172
  while (1)
173
    {
174
      unsigned long tmp = 0, nbits = 0;
175
      while (1)
176
        {
177
          tmp |= ((insn  >> (opd->type & OPTYPE_SHR)) & ((1 << opd->data) - 1)) << nbits;
178
          nbits += opd->data;
179
          if (opd->type & OPTYPE_OP)
180
            break;
181
          opd++;
182
        }
183
 
184
      /* Do we have to sign extend? */
185
      if (opd->type & OPTYPE_SIG)
186
        {
187
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
188
          if (tmp & (1 << sbit))
189
            tmp |= 0xFFFFFFFF << sbit;
190
        }
191
      if (opd->type & OPTYPE_DIS) {
192
        /* We have to read register later.  */
193
        data += tmp;
194
        dis = 1;
195
      } else
196
        {
197
          if (!(opd->type & OPTYPE_REG) || dis) {
198
            if (!dis) data = tmp;
199 703 markom
            if (strcmp (name, "l.movhi") == 0) {
200
              movhi = data << 16;
201
            } else {
202
              data |= movhi;
203
              //printf ("%08x %s\n", data, name);
204
              if (!(or32_opcodes[insn_index].flags & OR32_IF_DELAY)) {
205
                bcnt[bits(data)][0]++; bsum[0]++;
206
              } else {
207
                if (strcmp (name, "l.bf") == 0 || strcmp (name, "l.bnf") == 0) {
208
                  bcnt[bits(data)][1]++; bsum[1]++;
209
                } else {
210
                  bcnt[bits(data)][2]++; bsum[2]++;
211
                }
212
              }
213
            }
214 694 markom
          }
215
          data = 0;
216
          dis = 0;
217
        }
218
      if(opd->type & OPTYPE_LAST) {
219
        return;
220
      }
221
      opd++;
222
    }
223
}
224
#endif
225
 
226 28 lampret
char null_str[1] = "\0";
227
 
228 123 markom
/* Replaced several calls to translate(freemem) with vaddr */
229
/* Added new mode execution code */
230
/* Changed parameters so address can be passed as argument */
231 138 markom
void addprogram(unsigned long address, unsigned long insn, int* breakpoint)
232 2 cvs
{
233 138 markom
  char insn_first2_char[3];
234 361 markom
  int vaddr = (!runtime.sim.filename) ? translate(address,breakpoint) : translate(freemem,breakpoint);
235 138 markom
 
236
  setsim_mem32 (vaddr, insn);
237 694 markom
#if IMM_STATS
238
  check_insn (insn);
239
#endif
240 361 markom
  if(runtime.sim.filename)
241 138 markom
    freemem += insn_len (insn_decode (insn));
242 2 cvs
}
243
 
244 694 markom
/* Load big-endian COFF file.  */
245 28 lampret
 
246
void readfile_coff(char *filename, short sections)
247
{
248 361 markom
  FILE *inputfs;
249
  char inputbuf[4];
250
  unsigned long insn;
251
  signed long sectsize;
252
  COFF_AOUTHDR coffaouthdr;
253
  struct COFF_scnhdr coffscnhdr;
254
  int  len;
255
  char item[MAXLINE_LEN];
256
  char item2[MAXLINE_LEN];
257
  int  firstthree = 0;
258
  int breakpoint = 0;
259 123 markom
 
260 361 markom
  if (!(inputfs = fopen(filename, "r"))) {
261
    perror("readfile_coff");
262
    exit(1);
263
  }
264 28 lampret
 
265 361 markom
  if (fseek(inputfs, sizeof(struct COFF_filehdr), SEEK_SET) == -1) {
266
    fclose(inputfs);
267
    perror("readfile_coff");
268
    exit(1);
269
  }
270 28 lampret
 
271 361 markom
  if (fread(&coffaouthdr, sizeof(coffaouthdr), 1, inputfs) != 1) {
272
    fclose(inputfs);
273
    perror("readfile_coff");
274
    exit(1);
275
  }
276
 
277
  while(sections--) {
278
    long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
279
        + sizeof(struct COFF_scnhdr) * firstthree;
280
    if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
281
      fclose(inputfs);
282
      perror("readfile_coff");
283
      exit(1);
284
    }
285
    if (fread(&coffscnhdr, sizeof(struct COFF_scnhdr), 1, inputfs) != 1) {
286
      fclose(inputfs);
287
      perror("readfile_coff");
288
      exit(1);
289
    }
290
    printf("Section: %s,", coffscnhdr.s_name);
291
    printf(" paddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_paddr));
292
    printf(" vaddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_vaddr));
293
    printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
294
    printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
295
 
296
    sectsize = COFF_LONG_H(coffscnhdr.s_size);
297 221 markom
#if 0
298 361 markom
    /* A couple of sanity checks. */
299
    if (translate(COFF_LONG_H(coffscnhdr.s_vaddr),&breakpoint) < MEMORY_START) {
300
      printf("Section %s starts out of ", coffscnhdr.s_name);
301
      printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
302
      exit(1);
303
    }
304
    if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize,&breakpoint) >
305
        MEMORY_START + MEMORY_LEN) {
306
      printf("Section %s ends out of ", coffscnhdr.s_name);
307
      printf("memory.\n");
308
      exit(1);
309
    }
310 221 markom
#endif
311 167 markom
#if 0
312 361 markom
    if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
313
      printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
314
      exit(1);
315
    }
316
    if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
317
      printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
318
      exit(1);
319
    }
320
    if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
321
      printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
322
      exit(1);
323
    }
324 167 markom
#else
325 361 markom
    ++firstthree;
326 167 markom
#endif
327 28 lampret
 
328 361 markom
    /* loading section */
329
    freemem = COFF_LONG_H(coffscnhdr.s_paddr);
330
    debug(2,"Starting to load at 0x%x\n", freemem);
331
    if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
332
      fclose(inputfs);
333
      perror("readfile_coff");
334
      exit(1);
335
    }
336
    while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
337
      insn = COFF_LONG_H(inputbuf);
338
      len = insn_len (insn_decode (insn));
339
      if (len == 2)
340
        {
341
          fseek(inputfs, -2, SEEK_CUR);
342
          debug(8,"readfile_coff: %x 0x%x   \n", sectsize, insn >> 16);
343
        }
344
      else
345
        debug(8,"readfile_coff: %x 0x%x   \n", sectsize, insn);
346
      addprogram (freemem, insn, &breakpoint);
347
      sectsize -= len;
348
    }
349
  }
350
  if (firstthree < 3) {
351
    printf("One or more missing sections. At least");
352
    printf(" three sections expected (.text, .data, .bss).\n");
353
    exit(1);
354
  }
355
  if (firstthree > 3) {
356
    printf("Warning: one or more extra sections. These");
357
    printf(" sections were handled as .data sections.\n");
358
  }
359 41 lampret
 
360 361 markom
  fclose(inputfs);
361
  printf("Finished loading COFF.\n");
362
  return;
363 28 lampret
}
364
 
365
/* Load symbols from big-endian COFF file. */
366
 
367
void readsyms_coff(char *filename, unsigned long symptr, long syms)
368
{
369 173 markom
  FILE *inputfs;
370
  struct COFF_syment coffsymhdr;
371
  int breakpoint = 0;
372
  int count = 0;
373
  long nsyms = syms;
374
  if (!(inputfs = fopen(filename, "r"))) {
375
    perror("readsyms_coff");
376
    exit(1);
377
  }
378
 
379
  if (fseek(inputfs, symptr, SEEK_SET) == -1) {
380
    fclose(inputfs);
381
    perror("readsyms_coff");
382
    exit(1);
383
  }
384
 
385
  while(syms--) {
386
    int i, n;
387
    if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
388
      fclose(inputfs);
389
      perror("readsyms_coff");
390
      exit(1);
391
    }
392
 
393
    n = (unsigned char)coffsymhdr.e_numaux[0];
394 513 markom
 
395
    /* check whether this symbol belongs to a section and is external symbol; ignore all others */
396
    if (COFF_SHORT_H(coffsymhdr.e_scnum) >= 0 && coffsymhdr.e_sclass[0] == C_EXT) {
397
#if 0
398 173 markom
    /* If not important or not in text, skip. */
399 513 markom
    if(COFF_SHORT_H(coffsymhdr.e_type) & COFF_N_TMASK & COFF_STYP_TEXT) {
400
#endif
401
 
402 173 markom
      if (*((unsigned long *)coffsymhdr.e.e.e_zeroes)) {
403 513 markom
        if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
404
          add_label(COFF_LONG_H(coffsymhdr.e_value), coffsymhdr.e.e_name);
405
        debug(8, "[%i] Symbol: %s,", count++, coffsymhdr.e.e_name);
406 173 markom
      } else {
407 513 markom
        long fpos = ftell (inputfs);
408 361 markom
 
409 513 markom
        if (fseek(inputfs, symptr + nsyms * COFF_SYMESZ + COFF_LONG_H(coffsymhdr.e.e.e_offset), SEEK_SET) == 0) {
410
          char tmp[33], *s = &tmp[0];
411
          while (s != &tmp[32])
412
            if ((*(s++) = fgetc(inputfs)) == 0) break;
413
          tmp[32] = 0;
414
          add_label(COFF_LONG_H(coffsymhdr.e_value), &tmp[0]);
415
          debug(8, "[%i] Symbol: %s,", count++, &tmp[0]);
416
        }
417
        fseek(inputfs, fpos, SEEK_SET);
418 173 markom
      }
419
 
420 344 markom
      debug(9, " val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
421
      debug(9, " type: %x, %x, auxs: %i\n", COFF_SHORT_H(coffsymhdr.e_type), *((unsigned short *)coffsymhdr.e_type), n);
422 173 markom
    }
423 28 lampret
 
424 173 markom
    for (i = 0; i < n; i++)
425
      if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
426 513 markom
        fclose(inputfs);
427
        perror("readsyms_coff3");
428
        exit(1);
429 173 markom
      }
430
    syms -= n;
431
    count += n;
432
  }
433 28 lampret
 
434 173 markom
  fclose(inputfs);
435
  printf("Finished loading symbols.\n");
436
  return;
437 28 lampret
}
438
 
439 808 simons
void readfile_elf(char *filename)
440
{
441
 
442
  FILE *inputfs;
443
  struct elf32_hdr elfhdr;
444
  struct elf32_phdr *elf_phdata;
445
  struct elf32_shdr *elf_spnt, *elf_shdata;
446
  struct elf32_sym *sym_tbl = (struct elf32_sym *)0;
447
  unsigned long syms = 0;
448
  char *str_tbl = (char *)0;
449
  char *s_str = (char *)0;
450
  int breakpoint = 0;
451
  unsigned long inputbuf;
452
  unsigned long insn, padd;
453
  int i, j, sectsize, len;
454
 
455
  if (!(inputfs = fopen(filename, "r"))) {
456
    perror("readfile_elf");
457
    exit(1);
458
  }
459
 
460
  if (fread(&elfhdr, sizeof(elfhdr), 1, inputfs) != 1) {
461
    perror("readfile_elf");
462
    exit(1);
463
  }
464
 
465
  if ((elf_shdata = (struct elf32_shdr *)malloc(ELF_SHORT_H(elfhdr.e_shentsize) * ELF_SHORT_H(elfhdr.e_shnum))) == NULL) {
466
     perror("readfile_elf");
467
     exit(1);
468
  }
469
 
470
  if (fseek(inputfs, ELF_LONG_H(elfhdr.e_shoff), SEEK_SET) != 0) {
471
    perror("readfile_elf");
472
    exit(1);
473
  }
474
 
475
  if (fread(elf_shdata, ELF_SHORT_H(elfhdr.e_shentsize) * ELF_SHORT_H(elfhdr.e_shnum), 1, inputfs) != 1) {
476
    perror("readfile_elf");
477
    exit(1);
478
  }
479
 
480
  if (ELF_LONG_H(elfhdr.e_phoff)) {
481
 
482
    if((elf_phdata = (struct elf32_phdr *)malloc(ELF_SHORT_H(elfhdr.e_phnum) * ELF_SHORT_H(elfhdr.e_phentsize))) == NULL) {
483
      perror("readfile_elf");
484
      exit(1);
485
    }
486
 
487
    if (fseek(inputfs, ELF_LONG_H(elfhdr.e_phoff), SEEK_SET) != 0) {
488
      perror("readfile_elf");
489
      exit(1);
490
    }
491
 
492
    if (fread(elf_phdata, ELF_SHORT_H(elfhdr.e_phnum) * ELF_SHORT_H(elfhdr.e_phentsize), 1, inputfs) != 1) {
493
      perror("readfile_elf");
494
      exit(1);
495
    }
496
  }
497
 
498
  for(i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H(elfhdr.e_shnum); i++, elf_spnt++) {
499
 
500
    if(ELF_LONG_H(elf_spnt->sh_type) == SHT_STRTAB) {
501
 
502
      if((str_tbl = (char *)malloc(ELF_LONG_H(elf_spnt->sh_size))) == NULL) {
503
        perror("readfile_elf");
504
        exit(1);
505
      }
506
 
507
      if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
508
        perror("readfile_elf");
509
        exit(1);
510
      }
511
 
512
      if (fread(str_tbl, ELF_LONG_H(elf_spnt->sh_size), 1, inputfs) != 1) {
513
        perror("readfile_elf");
514
        exit(1);
515
      }
516
    }
517
    else if(ELF_LONG_H(elf_spnt->sh_type) == SHT_SYMTAB) {
518
 
519
      if((sym_tbl = (char *)malloc(ELF_LONG_H(elf_spnt->sh_size))) == NULL) {
520
        perror("readfile_elf");
521
        exit(1);
522
      }
523
 
524
      if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
525
        perror("readfile_elf");
526
        exit(1);
527
      }
528
 
529
      if (fread(sym_tbl, ELF_LONG_H(elf_spnt->sh_size), 1, inputfs) != 1) {
530
        perror("readfile_elf");
531
        exit(1);
532
      }
533
 
534
      syms = ELF_LONG_H(elf_spnt->sh_size) / ELF_LONG_H(elf_spnt->sh_entsize);
535
    }
536
  }
537
 
538
  if (ELF_SHORT_H(elfhdr.e_shstrndx) != SHN_UNDEF) {
539
    elf_spnt = &elf_shdata[ELF_SHORT_H(elfhdr.e_shstrndx)];
540
 
541
    if((s_str = (char *)malloc(ELF_LONG_H(elf_spnt->sh_size))) == NULL) {
542
      perror("readfile_elf");
543
      exit(1);
544
    }
545
 
546
    if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
547
      perror("readfile_elf");
548
      exit(1);
549
    }
550
 
551
    if (fread(s_str, ELF_LONG_H(elf_spnt->sh_size), 1, inputfs) != 1) {
552
      perror("readfile_elf");
553
      exit(1);
554
    }
555
  }
556
 
557
 
558
  for(i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H(elfhdr.e_shnum); i++, elf_spnt++) {
559
 
560
    if((ELF_LONG_H(elf_spnt->sh_type) & SHT_PROGBITS) && (ELF_LONG_H(elf_spnt->sh_flags) & SHF_ALLOC)) {
561
 
562
      padd = ELF_LONG_H(elf_spnt->sh_addr);
563
      for(j = 0; j < ELF_SHORT_H(elfhdr.e_phnum); j++) {
564
        if(ELF_LONG_H(elf_phdata[j].p_offset) &&
565
           ELF_LONG_H(elf_phdata[j].p_offset) <= ELF_LONG_H(elf_spnt->sh_offset) &&
566
          (ELF_LONG_H(elf_phdata[j].p_offset) + ELF_LONG_H(elf_phdata[j].p_memsz)) > ELF_LONG_H(elf_spnt->sh_offset))
567
          padd = ELF_LONG_H(elf_phdata[j].p_paddr) + ELF_LONG_H(elf_spnt->sh_offset) - ELF_LONG_H(elf_phdata[j].p_offset);
568
      }
569
 
570
 
571
 
572
      if (ELF_LONG_H(elf_spnt->sh_name) && s_str)
573
        printf("Section: %s,", &s_str[ELF_LONG_H(elf_spnt->sh_name)]);
574
      else
575
        printf("Section: noname,");
576
      printf(" vaddr: 0x%.8x,", ELF_LONG_H(elf_spnt->sh_addr));
577
      printf(" paddr: 0x%.8x,", padd);
578
      printf(" offset: 0x%.8x,", ELF_LONG_H(elf_spnt->sh_offset));
579
      printf(" size: 0x%.8x\n", ELF_LONG_H(elf_spnt->sh_size));
580
 
581
      freemem = ELF_LONG_H(elf_spnt->sh_addr);
582
      sectsize = ELF_LONG_H(elf_spnt->sh_size);
583
 
584
      if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
585
        perror("readfile_elf");
586
        exit(1);
587
      }
588
 
589
      while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
590
        insn = ELF_LONG_H(inputbuf);
591
        len = insn_len (insn_decode (insn));
592
        if (len == 2)
593
          {
594
            fseek(inputfs, -2, SEEK_CUR);
595
            debug(8, "readfile_elf: %x 0x%x   \n", sectsize, insn >> 16);
596
          }
597
        else
598
          debug(8, "readfile_elf: %x 0x%x   \n", sectsize, insn);
599
        addprogram (freemem, insn, &breakpoint);
600
        sectsize -= len;
601
      }
602
    }
603
  }
604
 
605
  if (str_tbl) {
606
    i = 0;
607
    while(syms--) {
608
      if (sym_tbl[i].st_name)
609
        add_label(ELF_LONG_H(sym_tbl[i].st_value), &str_tbl[ELF_LONG_H(sym_tbl[i].st_name)]);
610
      i++;
611
    }
612
  }
613
}
614
 
615 28 lampret
/* Identify file type and call appropriate readfile_X routine. It only
616
handles orX-coff-big executables at the moment. */
617
 
618
void identifyfile(char *filename)
619
{
620 361 markom
  FILE *inputfs;
621
  struct COFF_filehdr coffhdr;
622 808 simons
  struct elf32_hdr elfhdr;
623 361 markom
  size_t len;
624
 
625
  if (!(inputfs = fopen(filename, "r"))) {
626
    fprintf(stderr, "xx %s", filename);
627
    perror("identifyfile1");
628
    fflush(stdout);
629
    fflush(stderr);
630
    exit(1);
631
  }
632
 
633
  if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
634
    if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
635
          unsigned long opthdr_size;
636
      printf("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
637
      printf("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
638
      printf("COFF symptr: 0x%.8x\n", COFF_LONG_H(coffhdr.f_symptr));
639
      if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
640
        printf("This COFF is not an executable.\n");
641
        exit(1);
642
      }
643
      opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
644
      if (opthdr_size != sizeof(COFF_AOUTHDR)) {
645
        printf("COFF optional header is missing or not recognized.\n");
646
        printf("COFF f_opthdr: 0x%.2x\n", opthdr_size);
647
        exit(1);
648
      }
649
      fclose(inputfs);
650
      readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
651
      readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
652
      return;
653
    }
654
    else {
655 808 simons
      printf("Not COFF file format\n");
656
      fseek(inputfs, 0, SEEK_SET);
657
    }
658
  }
659
  if (fread(&elfhdr, sizeof(elfhdr), 1, inputfs) == 1) {
660
    if (elfhdr.e_ident[0] == 0x7f && elfhdr.e_ident[1] == 0x45 && elfhdr.e_ident[2] == 0x4c && elfhdr.e_ident[3] == 0x46) {
661
      printf("ELF type: 0x%.4x\n", ELF_SHORT_H(elfhdr.e_type));
662
      printf("ELF machine: 0x%.4x\n", ELF_SHORT_H(elfhdr.e_machine));
663
      printf("ELF version: 0x%.8x\n", ELF_LONG_H(elfhdr.e_version));
664
      printf("ELF sec = %d\n", ELF_SHORT_H(elfhdr.e_shnum));
665
      if (ELF_SHORT_H(elfhdr.e_type) != ET_EXEC ) {
666
        printf("This ELF is not an executable.\n");
667
        exit(1);
668
      }
669 361 markom
      fclose(inputfs);
670 808 simons
      readfile_elf(filename);
671
      return;
672 361 markom
    }
673 808 simons
    else {
674
      printf("Not ELF file format.\n");
675
      fseek(inputfs, 0, SEEK_SET);
676
    }
677 361 markom
  }
678 808 simons
 
679
  perror("identifyfile2");
680 361 markom
  fclose(inputfs);
681 28 lampret
 
682 361 markom
  return;
683 2 cvs
}
684
 
685 67 lampret
 
686
/* Loads file to memory starting at address startaddr and returns freemem. */
687
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
688 2 cvs
{
689 123 markom
  int breakpoint = 0;
690
 
691 361 markom
  transl_error = 0;
692
  transl_table = virtphy_transl;
693
  freemem = startaddr;
694 808 simons
  printf("loadcode: filename %s  startaddr=%x  virtphy_transl=%x\n", filename, startaddr, virtphy_transl);
695 361 markom
  identifyfile(filename);
696 694 markom
 
697
#if IMM_STATS  
698
  {
699 703 markom
    int i = 0, a = 0, b = 0, c = 0;
700
    printf ("index:arith/branch/jump\n");
701
    for (i = 0; i < 33; i++) printf ("%2i:\t%3.0f%% / %3.0f%%/ %3.0f%%\t%5i / %5i / %5i\n", i, 100.* (a += bcnt[i][0])/bsum[0],
702
        100.* (b += bcnt[i][1])/bsum[1], 100.* (c += bcnt[i][2])/bsum[2], bcnt[i][0], bcnt[i][1], bcnt[i][2]);
703
    printf ("\nsum %i %i %i\n", bsum[0], bsum[1], bsum[2]);
704 694 markom
  }
705
#endif
706
 
707 361 markom
  if (transl_error)
708
    return -1;
709
  else
710
    return translate(freemem,&breakpoint);
711 694 markom
 
712 2 cvs
}

powered by: WebSVN 2.1.0

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