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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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