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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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