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 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 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 1556 nogj
  /* We can't have set_program32 functions since it is not gauranteed that the
229
   * section we're loading is aligned on a 4-byte boundry */
230
  set_program8 (vaddr, (insn >> 24) & 0xff);
231
  set_program8 (vaddr + 1, (insn >> 16) & 0xff);
232
  set_program8 (vaddr + 2, (insn >> 8) & 0xff);
233
  set_program8 (vaddr + 3, insn & 0xff);
234 694 markom
#if IMM_STATS
235
  check_insn (insn);
236
#endif
237 361 markom
  if(runtime.sim.filename)
238 138 markom
    freemem += insn_len (insn_decode (insn));
239 2 cvs
}
240
 
241 694 markom
/* Load big-endian COFF file.  */
242 28 lampret
 
243
void readfile_coff(char *filename, short sections)
244
{
245 361 markom
  FILE *inputfs;
246
  char inputbuf[4];
247 1350 nogj
  uint32_t insn;
248 361 markom
  signed long sectsize;
249
  COFF_AOUTHDR coffaouthdr;
250
  struct COFF_scnhdr coffscnhdr;
251 1350 nogj
  int len;
252
  int firstthree = 0;
253 361 markom
  int breakpoint = 0;
254 123 markom
 
255 361 markom
  if (!(inputfs = fopen(filename, "r"))) {
256
    perror("readfile_coff");
257
    exit(1);
258
  }
259 28 lampret
 
260 361 markom
  if (fseek(inputfs, sizeof(struct COFF_filehdr), SEEK_SET) == -1) {
261
    fclose(inputfs);
262
    perror("readfile_coff");
263
    exit(1);
264
  }
265 28 lampret
 
266 361 markom
  if (fread(&coffaouthdr, sizeof(coffaouthdr), 1, inputfs) != 1) {
267
    fclose(inputfs);
268
    perror("readfile_coff");
269
    exit(1);
270
  }
271
 
272
  while(sections--) {
273
    long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
274
        + sizeof(struct COFF_scnhdr) * firstthree;
275
    if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
276
      fclose(inputfs);
277
      perror("readfile_coff");
278
      exit(1);
279
    }
280
    if (fread(&coffscnhdr, sizeof(struct COFF_scnhdr), 1, inputfs) != 1) {
281
      fclose(inputfs);
282
      perror("readfile_coff");
283
      exit(1);
284
    }
285 997 markom
    PRINTF("Section: %s,", coffscnhdr.s_name);
286 1308 phoenix
    PRINTF(" paddr: 0x%.8lx,", COFF_LONG_H(coffscnhdr.s_paddr));
287
    PRINTF(" vaddr: 0x%.8lx,", COFF_LONG_H(coffscnhdr.s_vaddr));
288
    PRINTF(" size: 0x%.8lx,", COFF_LONG_H(coffscnhdr.s_size));
289
    PRINTF(" scnptr: 0x%.8lx\n", COFF_LONG_H(coffscnhdr.s_scnptr));
290 361 markom
 
291
    sectsize = COFF_LONG_H(coffscnhdr.s_size);
292 221 markom
#if 0
293 361 markom
    /* A couple of sanity checks. */
294
    if (translate(COFF_LONG_H(coffscnhdr.s_vaddr),&breakpoint) < MEMORY_START) {
295 997 markom
      PRINTF("Section %s starts out of ", coffscnhdr.s_name);
296
      PRINTF("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
297 361 markom
      exit(1);
298
    }
299
    if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize,&breakpoint) >
300
        MEMORY_START + MEMORY_LEN) {
301 997 markom
      PRINTF("Section %s ends out of ", coffscnhdr.s_name);
302
      PRINTF("memory.\n");
303 361 markom
      exit(1);
304
    }
305 221 markom
#endif
306 167 markom
#if 0
307 361 markom
    if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
308 997 markom
      PRINTF("First section should be .text (%s instead)\n", coffscnhdr.s_name);
309 361 markom
      exit(1);
310
    }
311
    if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
312 997 markom
      PRINTF("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
313 361 markom
      exit(1);
314
    }
315
    if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
316 997 markom
      PRINTF("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
317 361 markom
      exit(1);
318
    }
319 167 markom
#else
320 361 markom
    ++firstthree;
321 167 markom
#endif
322 28 lampret
 
323 361 markom
    /* loading section */
324
    freemem = COFF_LONG_H(coffscnhdr.s_paddr);
325
    debug(2,"Starting to load at 0x%x\n", freemem);
326
    if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
327
      fclose(inputfs);
328
      perror("readfile_coff");
329
      exit(1);
330
    }
331
    while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
332
      insn = COFF_LONG_H(inputbuf);
333
      len = insn_len (insn_decode (insn));
334
      if (len == 2)
335
        {
336
          fseek(inputfs, -2, SEEK_CUR);
337 1350 nogj
          debug(8,"readfile_coff: %lx 0x%x   \n", sectsize, insn >> 16);
338 361 markom
        }
339
      else
340 1350 nogj
        debug(8,"readfile_coff: %lx 0x%x   \n", sectsize, insn);
341 361 markom
      addprogram (freemem, insn, &breakpoint);
342
      sectsize -= len;
343
    }
344
  }
345
  if (firstthree < 3) {
346 997 markom
    PRINTF("One or more missing sections. At least");
347
    PRINTF(" three sections expected (.text, .data, .bss).\n");
348 361 markom
    exit(1);
349
  }
350
  if (firstthree > 3) {
351 997 markom
    PRINTF("Warning: one or more extra sections. These");
352
    PRINTF(" sections were handled as .data sections.\n");
353 361 markom
  }
354 41 lampret
 
355 361 markom
  fclose(inputfs);
356 997 markom
  PRINTF("Finished loading COFF.\n");
357 361 markom
  return;
358 28 lampret
}
359
 
360
/* Load symbols from big-endian COFF file. */
361
 
362
void readsyms_coff(char *filename, unsigned long symptr, long syms)
363
{
364 173 markom
  FILE *inputfs;
365
  struct COFF_syment coffsymhdr;
366
  int count = 0;
367
  long nsyms = syms;
368
  if (!(inputfs = fopen(filename, "r"))) {
369
    perror("readsyms_coff");
370
    exit(1);
371
  }
372
 
373
  if (fseek(inputfs, symptr, SEEK_SET) == -1) {
374
    fclose(inputfs);
375
    perror("readsyms_coff");
376
    exit(1);
377
  }
378
 
379
  while(syms--) {
380
    int i, n;
381
    if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
382
      fclose(inputfs);
383
      perror("readsyms_coff");
384
      exit(1);
385
    }
386
 
387
    n = (unsigned char)coffsymhdr.e_numaux[0];
388 513 markom
 
389
    /* check whether this symbol belongs to a section and is external symbol; ignore all others */
390
    if (COFF_SHORT_H(coffsymhdr.e_scnum) >= 0 && coffsymhdr.e_sclass[0] == C_EXT) {
391
#if 0
392 173 markom
    /* If not important or not in text, skip. */
393 513 markom
    if(COFF_SHORT_H(coffsymhdr.e_type) & COFF_N_TMASK & COFF_STYP_TEXT) {
394
#endif
395
 
396 173 markom
      if (*((unsigned long *)coffsymhdr.e.e.e_zeroes)) {
397 513 markom
        if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
398
          add_label(COFF_LONG_H(coffsymhdr.e_value), coffsymhdr.e.e_name);
399
        debug(8, "[%i] Symbol: %s,", count++, coffsymhdr.e.e_name);
400 173 markom
      } else {
401 513 markom
        long fpos = ftell (inputfs);
402 361 markom
 
403 513 markom
        if (fseek(inputfs, symptr + nsyms * COFF_SYMESZ + COFF_LONG_H(coffsymhdr.e.e.e_offset), SEEK_SET) == 0) {
404
          char tmp[33], *s = &tmp[0];
405
          while (s != &tmp[32])
406
            if ((*(s++) = fgetc(inputfs)) == 0) break;
407
          tmp[32] = 0;
408
          add_label(COFF_LONG_H(coffsymhdr.e_value), &tmp[0]);
409
          debug(8, "[%i] Symbol: %s,", count++, &tmp[0]);
410
        }
411
        fseek(inputfs, fpos, SEEK_SET);
412 173 markom
      }
413
 
414 1350 nogj
      debug(9, " val: 0x%.8lx,", COFF_LONG_H(coffsymhdr.e_value));
415 344 markom
      debug(9, " type: %x, %x, auxs: %i\n", COFF_SHORT_H(coffsymhdr.e_type), *((unsigned short *)coffsymhdr.e_type), n);
416 173 markom
    }
417 28 lampret
 
418 173 markom
    for (i = 0; i < n; i++)
419
      if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
420 513 markom
        fclose(inputfs);
421
        perror("readsyms_coff3");
422
        exit(1);
423 173 markom
      }
424
    syms -= n;
425
    count += n;
426
  }
427 28 lampret
 
428 173 markom
  fclose(inputfs);
429 997 markom
  PRINTF("Finished loading symbols.\n");
430 173 markom
  return;
431 28 lampret
}
432
 
433 808 simons
void readfile_elf(char *filename)
434
{
435
 
436
  FILE *inputfs;
437
  struct elf32_hdr elfhdr;
438 1557 nogj
  struct elf32_phdr *elf_phdata = NULL;
439 808 simons
  struct elf32_shdr *elf_spnt, *elf_shdata;
440
  struct elf32_sym *sym_tbl = (struct elf32_sym *)0;
441
  unsigned long syms = 0;
442
  char *str_tbl = (char *)0;
443
  char *s_str = (char *)0;
444
  int breakpoint = 0;
445
  unsigned long inputbuf;
446 1350 nogj
  unsigned long padd;
447
  uint32_t insn;
448 808 simons
  int i, j, sectsize, len;
449
 
450
  if (!(inputfs = fopen(filename, "r"))) {
451
    perror("readfile_elf");
452
    exit(1);
453
  }
454
 
455
  if (fread(&elfhdr, sizeof(elfhdr), 1, inputfs) != 1) {
456
    perror("readfile_elf");
457
    exit(1);
458
  }
459
 
460
  if ((elf_shdata = (struct elf32_shdr *)malloc(ELF_SHORT_H(elfhdr.e_shentsize) * ELF_SHORT_H(elfhdr.e_shnum))) == NULL) {
461
     perror("readfile_elf");
462
     exit(1);
463
  }
464
 
465
  if (fseek(inputfs, ELF_LONG_H(elfhdr.e_shoff), SEEK_SET) != 0) {
466
    perror("readfile_elf");
467
    exit(1);
468
  }
469
 
470
  if (fread(elf_shdata, ELF_SHORT_H(elfhdr.e_shentsize) * ELF_SHORT_H(elfhdr.e_shnum), 1, inputfs) != 1) {
471
    perror("readfile_elf");
472
    exit(1);
473
  }
474
 
475
  if (ELF_LONG_H(elfhdr.e_phoff)) {
476
 
477
    if((elf_phdata = (struct elf32_phdr *)malloc(ELF_SHORT_H(elfhdr.e_phnum) * ELF_SHORT_H(elfhdr.e_phentsize))) == NULL) {
478
      perror("readfile_elf");
479
      exit(1);
480
    }
481
 
482
    if (fseek(inputfs, ELF_LONG_H(elfhdr.e_phoff), SEEK_SET) != 0) {
483
      perror("readfile_elf");
484
      exit(1);
485
    }
486
 
487
    if (fread(elf_phdata, ELF_SHORT_H(elfhdr.e_phnum) * ELF_SHORT_H(elfhdr.e_phentsize), 1, inputfs) != 1) {
488
      perror("readfile_elf");
489
      exit(1);
490
    }
491
  }
492
 
493
  for(i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H(elfhdr.e_shnum); i++, elf_spnt++) {
494
 
495
    if(ELF_LONG_H(elf_spnt->sh_type) == SHT_STRTAB) {
496
 
497
      if((str_tbl = (char *)malloc(ELF_LONG_H(elf_spnt->sh_size))) == NULL) {
498
        perror("readfile_elf");
499
        exit(1);
500
      }
501
 
502
      if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
503
        perror("readfile_elf");
504
        exit(1);
505
      }
506
 
507
      if (fread(str_tbl, ELF_LONG_H(elf_spnt->sh_size), 1, inputfs) != 1) {
508
        perror("readfile_elf");
509
        exit(1);
510
      }
511
    }
512
    else if(ELF_LONG_H(elf_spnt->sh_type) == SHT_SYMTAB) {
513
 
514 897 markom
      if((sym_tbl = (struct elf32_sym *)malloc(ELF_LONG_H(elf_spnt->sh_size))) == NULL) {
515 808 simons
        perror("readfile_elf");
516
        exit(1);
517
      }
518
 
519
      if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
520
        perror("readfile_elf");
521
        exit(1);
522
      }
523
 
524
      if (fread(sym_tbl, ELF_LONG_H(elf_spnt->sh_size), 1, inputfs) != 1) {
525
        perror("readfile_elf");
526
        exit(1);
527
      }
528
 
529
      syms = ELF_LONG_H(elf_spnt->sh_size) / ELF_LONG_H(elf_spnt->sh_entsize);
530
    }
531
  }
532
 
533
  if (ELF_SHORT_H(elfhdr.e_shstrndx) != SHN_UNDEF) {
534
    elf_spnt = &elf_shdata[ELF_SHORT_H(elfhdr.e_shstrndx)];
535
 
536
    if((s_str = (char *)malloc(ELF_LONG_H(elf_spnt->sh_size))) == NULL) {
537
      perror("readfile_elf");
538
      exit(1);
539
    }
540
 
541
    if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
542
      perror("readfile_elf");
543
      exit(1);
544
    }
545
 
546
    if (fread(s_str, ELF_LONG_H(elf_spnt->sh_size), 1, inputfs) != 1) {
547
      perror("readfile_elf");
548
      exit(1);
549
    }
550
  }
551
 
552
 
553
  for(i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H(elfhdr.e_shnum); i++, elf_spnt++) {
554
 
555
    if((ELF_LONG_H(elf_spnt->sh_type) & SHT_PROGBITS) && (ELF_LONG_H(elf_spnt->sh_flags) & SHF_ALLOC)) {
556
 
557
      padd = ELF_LONG_H(elf_spnt->sh_addr);
558
      for(j = 0; j < ELF_SHORT_H(elfhdr.e_phnum); j++) {
559
        if(ELF_LONG_H(elf_phdata[j].p_offset) &&
560
           ELF_LONG_H(elf_phdata[j].p_offset) <= ELF_LONG_H(elf_spnt->sh_offset) &&
561
          (ELF_LONG_H(elf_phdata[j].p_offset) + ELF_LONG_H(elf_phdata[j].p_memsz)) > ELF_LONG_H(elf_spnt->sh_offset))
562
          padd = ELF_LONG_H(elf_phdata[j].p_paddr) + ELF_LONG_H(elf_spnt->sh_offset) - ELF_LONG_H(elf_phdata[j].p_offset);
563
      }
564
 
565
 
566
 
567
      if (ELF_LONG_H(elf_spnt->sh_name) && s_str)
568 997 markom
        PRINTF("Section: %s,", &s_str[ELF_LONG_H(elf_spnt->sh_name)]);
569 808 simons
      else
570 997 markom
        PRINTF("Section: noname,");
571 1308 phoenix
      PRINTF(" vaddr: 0x%.8lx,", ELF_LONG_H(elf_spnt->sh_addr));
572
      PRINTF(" paddr: 0x%.8lx,", padd);
573
      PRINTF(" offset: 0x%.8lx,", ELF_LONG_H(elf_spnt->sh_offset));
574
      PRINTF(" size: 0x%.8lx\n", ELF_LONG_H(elf_spnt->sh_size));
575 808 simons
 
576 819 simons
      freemem = padd;
577 808 simons
      sectsize = ELF_LONG_H(elf_spnt->sh_size);
578
 
579
      if (fseek(inputfs, ELF_LONG_H(elf_spnt->sh_offset), SEEK_SET) != 0) {
580
        perror("readfile_elf");
581
        exit(1);
582
      }
583
 
584
      while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
585
        insn = ELF_LONG_H(inputbuf);
586
        len = insn_len (insn_decode (insn));
587
        if (len == 2)
588
          {
589
            fseek(inputfs, -2, SEEK_CUR);
590
            debug(8, "readfile_elf: %x 0x%x   \n", sectsize, insn >> 16);
591
          }
592
        else
593
          debug(8, "readfile_elf: %x 0x%x   \n", sectsize, insn);
594
        addprogram (freemem, insn, &breakpoint);
595
        sectsize -= len;
596
      }
597
    }
598
  }
599
 
600
  if (str_tbl) {
601
    i = 0;
602
    while(syms--) {
603 1061 markom
      if (sym_tbl[i].st_name && sym_tbl[i].st_info && ELF_SHORT_H(sym_tbl[i].st_shndx) < 0x8000) {
604 808 simons
        add_label(ELF_LONG_H(sym_tbl[i].st_value), &str_tbl[ELF_LONG_H(sym_tbl[i].st_name)]);
605 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));
606 1061 markom
      }
607 808 simons
      i++;
608
    }
609
  }
610
}
611
 
612 28 lampret
/* Identify file type and call appropriate readfile_X routine. It only
613
handles orX-coff-big executables at the moment. */
614
 
615
void identifyfile(char *filename)
616
{
617 361 markom
  FILE *inputfs;
618
  struct COFF_filehdr coffhdr;
619 808 simons
  struct elf32_hdr elfhdr;
620 361 markom
 
621
  if (!(inputfs = fopen(filename, "r"))) {
622 883 markom
    perror(filename);
623 361 markom
    fflush(stdout);
624
    fflush(stderr);
625
    exit(1);
626
  }
627
 
628
  if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
629
    if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
630
          unsigned long opthdr_size;
631 997 markom
      PRINTF("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
632
      PRINTF("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
633 1308 phoenix
      PRINTF("COFF symptr: 0x%.8lx\n", COFF_LONG_H(coffhdr.f_symptr));
634 361 markom
      if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
635 997 markom
        PRINTF("This COFF is not an executable.\n");
636 361 markom
        exit(1);
637
      }
638
      opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
639
      if (opthdr_size != sizeof(COFF_AOUTHDR)) {
640 997 markom
        PRINTF("COFF optional header is missing or not recognized.\n");
641 1308 phoenix
        PRINTF("COFF f_opthdr: 0x%.2lx\n", opthdr_size);
642 361 markom
        exit(1);
643
      }
644
      fclose(inputfs);
645
      readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
646
      readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
647
      return;
648
    }
649
    else {
650 997 markom
      PRINTF("Not COFF file format\n");
651 808 simons
      fseek(inputfs, 0, SEEK_SET);
652
    }
653
  }
654
  if (fread(&elfhdr, sizeof(elfhdr), 1, inputfs) == 1) {
655
    if (elfhdr.e_ident[0] == 0x7f && elfhdr.e_ident[1] == 0x45 && elfhdr.e_ident[2] == 0x4c && elfhdr.e_ident[3] == 0x46) {
656 997 markom
      PRINTF("ELF type: 0x%.4x\n", ELF_SHORT_H(elfhdr.e_type));
657
      PRINTF("ELF machine: 0x%.4x\n", ELF_SHORT_H(elfhdr.e_machine));
658 1308 phoenix
      PRINTF("ELF version: 0x%.8lx\n", ELF_LONG_H(elfhdr.e_version));
659 997 markom
      PRINTF("ELF sec = %d\n", ELF_SHORT_H(elfhdr.e_shnum));
660 808 simons
      if (ELF_SHORT_H(elfhdr.e_type) != ET_EXEC ) {
661 997 markom
        PRINTF("This ELF is not an executable.\n");
662 808 simons
        exit(1);
663
      }
664 361 markom
      fclose(inputfs);
665 808 simons
      readfile_elf(filename);
666
      return;
667 361 markom
    }
668 808 simons
    else {
669 997 markom
      PRINTF("Not ELF file format.\n");
670 808 simons
      fseek(inputfs, 0, SEEK_SET);
671
    }
672 361 markom
  }
673 808 simons
 
674
  perror("identifyfile2");
675 361 markom
  fclose(inputfs);
676 28 lampret
 
677 361 markom
  return;
678 2 cvs
}
679
 
680 67 lampret
 
681
/* Loads file to memory starting at address startaddr and returns freemem. */
682 1350 nogj
unsigned long loadcode(char *filename, oraddr_t startaddr, oraddr_t virtphy_transl)
683 2 cvs
{
684 123 markom
  int breakpoint = 0;
685
 
686 361 markom
  transl_error = 0;
687
  transl_table = virtphy_transl;
688
  freemem = startaddr;
689 1350 nogj
  PRINTF("loadcode: filename %s  startaddr=%"PRIxADDR"  virtphy_transl=%"PRIxADDR"\n",
690
         filename, startaddr, virtphy_transl);
691 361 markom
  identifyfile(filename);
692 694 markom
 
693
#if IMM_STATS  
694
  {
695 703 markom
    int i = 0, a = 0, b = 0, c = 0;
696 997 markom
    PRINTF ("index:arith/branch/jump\n");
697 1350 nogj
    for (i = 0; i < 33; i++)
698
      PRINTF ("%2i:\t%3.0f%% / %3.0f%%/ %3.0f%%\t%5i / %5i / %5i\n", i,
699
              100.* (a += bcnt[i][0])/bsum[0], 100.* (b += bcnt[i][1])/bsum[1],
700
              100.* (c += bcnt[i][2])/bsum[2], bcnt[i][0], bcnt[i][1], bcnt[i][2]);
701 997 markom
    PRINTF ("\nsum %i %i %i\n", bsum[0], bsum[1], bsum[2]);
702 694 markom
  }
703
#endif
704
 
705 361 markom
  if (transl_error)
706
    return -1;
707
  else
708
    return translate(freemem,&breakpoint);
709 694 markom
 
710 2 cvs
}

powered by: WebSVN 2.1.0

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