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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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