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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [cpu/] [common/] [parse.c] - Blame information for rev 1487

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

powered by: WebSVN 2.1.0

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