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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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