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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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