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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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