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 304

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
#include "abstract.h"
26 18 lampret
#include "arch.h"
27 67 lampret
#include "dmmu.h"
28 28 lampret
#include "coff.h"
29 138 markom
#include "debug_unit.h"
30 134 markom
#include "opcode/or32.h"
31 138 markom
#include "parse.h"
32 242 markom
#include "sim-config.h"
33 2 cvs
 
34 221 markom
#define MEMORY_LEN 0x100000000
35 2 cvs
#define MAXLINE_LEN     18000
36
 
37 28 lampret
extern char *disassembled;
38
 
39 2 cvs
/* Unused mem memory marker. It is used when allocating program and data memory
40
   during parsing */
41
unsigned int freemem;
42
 
43 67 lampret
/* Translation table provided by microkernel. Only used if simulating microkernel. */
44
static unsigned long transl_table;
45
 
46
/* Used to signal whether during loading of programs a translation fault occured. */
47
static unsigned long transl_error;
48
 
49
 
50 2 cvs
char *strtoken(char *in, char *out, int which)
51
{
52
        char    *super;
53
        char    *sub;
54
        char    *newline;
55
 
56
        super = strdup(in);
57
        sub = strtok(super, " \t");
58
        while (sub && --which)
59
                sub = strtok(NULL, " \t");
60
        if (sub && !which) {
61
                if ((newline = strchr(sub, '\n')))
62
                        newline[0] = '\0';
63
                strcpy(out, sub);
64
        } else
65
                out[0] = '\0';
66
        free(super);
67
        if ((newline = strchr(out, '\r')))      /* get rid of CR */
68
                newline[0] = '\0';
69
        return(out);
70
}
71
 
72 304 markom
char *
73
stripwhite (string)
74
     char *string;
75
{
76
  register char *s, *t;
77
 
78
  for (s = string; whitespace (*s); s++)
79
    ;
80
 
81
  if (*s == 0)
82
    return (s);
83
 
84
  t = s + strlen (s) - 1;
85
  while (t > s && whitespace (*t))
86
    t--;
87
  *++t = '\0';
88
 
89
  return s;
90
}
91
 
92
char *
93
dupstr (s)
94
     char *s;
95
{
96
  char *r;
97
 
98
  r = xmalloc (strlen (s) + 1);
99
  strcpy (r, s);
100
  return (r);
101
}
102
 
103 221 markom
/* Used only by the simulator loader to translate logical addresses into physical.
104 67 lampret
   If loadcode() is called with valid virtphy_transl pointer to a table of
105
   translations then translate() performs translation otherwise phy address is
106
   equal to logical. */
107 123 markom
static unsigned int translate(unsigned int laddr,int* breakpoint)
108 67 lampret
{
109
        int i;
110
 
111
        /* No translation (i.e. when loading kernel into simulator)
112
/*      printf("transl_table=%x  laddr=%x\n", transl_table, laddr);
113
        printf("laddr=%x\n", laddr);*/
114
        if (transl_table == 0)
115
                return laddr;
116
 
117
        /* Try to find our translation in the table. */
118
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
119 221 markom
                if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i)) {
120 67 lampret
                        setsim_mem32(transl_table + i + 8, -2); /* Page modified */
121 221 markom
                        printf("found paddr=%x\n", evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
122
                        return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
123 67 lampret
                }
124
 
125
        /* Allocate new phy page for us. */
126
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
127 221 markom
                if (evalsim_mem32(transl_table + i + 8) == 0) {
128 67 lampret
                        setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
129
                        setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
130
                        setsim_mem32(transl_table + i + 8, -2); /* Page modified */
131 221 markom
                        printf("newly allocated ppn=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4));
132 67 lampret
                        printf("newly allocated .ppn=%x\n", (unsigned long)transl_table + i + 4);
133
                        printf("newly allocated ofs=%x\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
134 221 markom
                        printf("newly allocated paddr=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
135
                        return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
136 67 lampret
                }
137
        /* If we come this far then all phy memory is used and we can't find our page
138
           nor allocate new page. */
139
        transl_error = 1;
140
 
141
        printf("can't translate\n", laddr);
142
        exit(1);
143
        return -1;
144
}
145
 
146 28 lampret
char null_str[1] = "\0";
147
 
148 123 markom
/* Modified by CZ 26/05/01 */
149
/* Replaced several calls to translate(freemem) with vaddr */
150
/* Added new mode execution code */
151
/* Changed parameters so address can be passed as argument */
152 138 markom
void addprogram(unsigned long address, unsigned long insn, int* breakpoint)
153 2 cvs
{
154 138 markom
  char insn_first2_char[3];
155 242 markom
  int vaddr = (!config.filename) ? translate(address,breakpoint) : translate(freemem,breakpoint);
156 138 markom
 
157
  debug("addprogram 1\n");
158 18 lampret
 
159 138 markom
  setsim_mem32 (vaddr, insn);
160 2 cvs
 
161 242 markom
  if(config.filename)
162 138 markom
    freemem += insn_len (insn_decode (insn));
163 2 cvs
}
164
 
165 28 lampret
/* Load big-endian COFF file. At the moment it doesn't load symbols yet. */
166
 
167
void readfile_coff(char *filename, short sections)
168
{
169
        FILE *inputfs;
170
        char inputbuf[4];
171
        unsigned long insn;
172 41 lampret
        signed long sectsize;
173 28 lampret
        COFF_AOUTHDR coffaouthdr;
174
        struct COFF_scnhdr coffscnhdr;
175
        int  len;
176
        char item[MAXLINE_LEN];
177
        char item2[MAXLINE_LEN];
178 41 lampret
        int  firstthree = 0;
179 123 markom
        int breakpoint = 0;
180
 
181 28 lampret
        if (!(inputfs = fopen(filename, "r"))) {
182
                perror("readfile_coff");
183
                exit(1);
184
        }
185
 
186
        if (fseek(inputfs, sizeof(struct COFF_filehdr), SEEK_SET) == -1) {
187
                fclose(inputfs);
188
                perror("readfile_coff");
189
                exit(1);
190
        }
191
 
192
        if (fread(&coffaouthdr, sizeof(coffaouthdr), 1, inputfs) != 1) {
193
                fclose(inputfs);
194
                perror("readfile_coff");
195
                exit(1);
196
        }
197
 
198
        while(sections--) {
199 41 lampret
                long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
200
                                + sizeof(struct COFF_scnhdr) * firstthree;
201
                if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
202
                        fclose(inputfs);
203
                        perror("readfile_coff");
204
                        exit(1);
205
                }
206 28 lampret
                if (fread(&coffscnhdr, sizeof(struct COFF_scnhdr), 1, inputfs) != 1) {
207
                        fclose(inputfs);
208
                        perror("readfile_coff");
209
                        exit(1);
210
                }
211
                printf("Section: %s,", coffscnhdr.s_name);
212 296 simons
                printf(" paddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_paddr));
213 41 lampret
                printf(" vaddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_vaddr));
214
                printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
215
                printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
216
 
217
                sectsize = COFF_LONG_H(coffscnhdr.s_size);
218 221 markom
#if 0
219 41 lampret
                /* A couple of sanity checks. */
220 123 markom
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr),&breakpoint) < MEMORY_START) {
221 41 lampret
                        printf("Section %s starts out of ", coffscnhdr.s_name);
222
                        printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
223
                        exit(1);
224
                }
225 123 markom
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize,&breakpoint) >
226 41 lampret
                    MEMORY_START + MEMORY_LEN) {
227
                        printf("Section %s ends out of ", coffscnhdr.s_name);
228
                        printf("memory.\n");
229
                        exit(1);
230
                }
231 221 markom
#endif
232 167 markom
#if 0
233 41 lampret
                if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
234
                        printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
235
                        exit(1);
236
                }
237
                if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
238
                        printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
239
                        exit(1);
240
                }
241
                if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
242
                        printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
243
                        exit(1);
244
                }
245 167 markom
#else
246
                ++firstthree;
247
#endif
248 28 lampret
 
249 41 lampret
                /* loading section */
250 296 simons
                freemem = COFF_LONG_H(coffscnhdr.s_paddr);
251 67 lampret
                debug("Starting to load at 0x%x", freemem);
252 41 lampret
                if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
253
                        fclose(inputfs);
254
                        perror("readfile_coff");
255
                        exit(1);
256 28 lampret
                }
257 41 lampret
                while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
258
                        insn = COFF_LONG_H(inputbuf);
259 138 markom
                        len = insn_len (insn_decode (insn));
260
                        if (len == 2)
261
                          {
262
                            fseek(inputfs, -2, SEEK_CUR);
263
                            debug("readfile_coff: %x 0x%x   ", sectsize, insn >> 16);
264
                          }
265 41 lampret
                        else
266 138 markom
                          debug("readfile_coff: %x 0x%x   ", sectsize, insn);
267
                        addprogram (freemem, insn, &breakpoint);
268 41 lampret
                        sectsize -= len;
269 138 markom
                }
270 28 lampret
        }
271 41 lampret
        if (firstthree < 3) {
272
                printf("One or more missing sections. At least");
273
                printf(" three sections expected (.text, .data, .bss).\n");
274
                exit(1);
275
        }
276
        if (firstthree > 3) {
277
                printf("Warning: one or more extra sections. These");
278
                printf(" sections were handled as .data sections.\n");
279
        }
280
 
281 28 lampret
        fclose(inputfs);
282
        printf("Finished loading COFF.\n");
283
        return;
284
}
285
 
286
/* Load symbols from big-endian COFF file. */
287
 
288
void readsyms_coff(char *filename, unsigned long symptr, long syms)
289
{
290 173 markom
  FILE *inputfs;
291
  struct COFF_syment coffsymhdr;
292
  int breakpoint = 0;
293
  int count = 0;
294
  long nsyms = syms;
295
  if (!(inputfs = fopen(filename, "r"))) {
296
    perror("readsyms_coff");
297
    exit(1);
298
  }
299
 
300
  if (fseek(inputfs, symptr, SEEK_SET) == -1) {
301
    fclose(inputfs);
302
    perror("readsyms_coff");
303
    exit(1);
304
  }
305
 
306
  while(syms--) {
307
    int i, n;
308
    if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
309
      fclose(inputfs);
310
      perror("readsyms_coff");
311
      exit(1);
312
    }
313
 
314
    n = (unsigned char)coffsymhdr.e_numaux[0];
315
    /* If not important or not in text, skip. */
316
    if (COFF_SHORT_H(coffsymhdr.e_type) & COFF_N_TMASK & COFF_STYP_TEXT) {
317
 
318
      if (*((unsigned long *)coffsymhdr.e.e.e_zeroes)) {
319
        if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
320 261 markom
          add_label(COFF_LONG_H(coffsymhdr.e_value), coffsymhdr.e.e_name);
321 173 markom
        debug("[%i] Symbol: %s,", count++, coffsymhdr.e.e_name);
322
      } else {
323
        long fpos = ftell (inputfs);
324 28 lampret
 
325 173 markom
        if (fseek(inputfs, symptr + nsyms * COFF_SYMESZ + COFF_LONG_H(coffsymhdr.e.e.e_offset), SEEK_SET) == 0) {
326
          char tmp[33], *s = &tmp[0];
327
          while (s != &tmp[32])
328
            if ((*(s++) = fgetc(inputfs)) == 0) break;
329
          tmp[32] = 0;
330 261 markom
          add_label(COFF_LONG_H(coffsymhdr.e_value), &tmp[0]);
331 173 markom
          debug("[%i] Symbol: %s,", count++, &tmp[0]);
332 28 lampret
        }
333 173 markom
        fseek(inputfs, fpos, SEEK_SET);
334
      }
335
 
336
      debug(" val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
337
      debug(" type: %x, %x, auxs: %i\n", COFF_SHORT_H(coffsymhdr.e_type), *((unsigned short *)coffsymhdr.e_type), n);
338
    }
339 28 lampret
 
340 173 markom
    for (i = 0; i < n; i++)
341
      if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
342
        fclose(inputfs);
343
        perror("readsyms_coff3");
344
        exit(1);
345
      }
346
    syms -= n;
347
    count += n;
348
  }
349 28 lampret
 
350 173 markom
  fclose(inputfs);
351
  printf("Finished loading symbols.\n");
352
  return;
353 28 lampret
}
354
 
355
/* Identify file type and call appropriate readfile_X routine. It only
356
handles orX-coff-big executables at the moment. */
357
 
358
void identifyfile(char *filename)
359
{
360
        FILE *inputfs;
361
        struct COFF_filehdr coffhdr;
362
        size_t len;
363
 
364
        if (!(inputfs = fopen(filename, "r"))) {
365 67 lampret
                fprintf(stderr, "xx %s", filename);
366
                perror("identifyfile1");
367
                fflush(stdout);
368
                fflush(stderr);
369 28 lampret
                exit(1);
370
        }
371
 
372
        if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
373
                if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
374
                        unsigned long opthdr_size;
375
                        printf("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
376
                        printf("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
377
                        printf("COFF symptr: 0x%.8x\n", COFF_LONG_H(coffhdr.f_symptr));
378
                        if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
379
                                printf("This COFF is not an executable.\n");
380
                                exit(1);
381
                        }
382
                        opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
383
                        if (opthdr_size != sizeof(COFF_AOUTHDR)) {
384
                                printf("COFF optional header is missing or not recognized.\n");
385
                                printf("COFF f_opthdr: 0x%.2x\n", opthdr_size);
386
                                exit(1);
387
                        }
388
                        fclose(inputfs);
389
                        readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
390
                        readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
391
                        return;
392 2 cvs
                }
393 28 lampret
                else {
394 138 markom
                        printf("Not COFF, quiting.\n");
395 28 lampret
                        fclose(inputfs);
396 138 markom
                        exit (1);
397 28 lampret
                }
398 2 cvs
        }
399 67 lampret
        else {
400
                printf("yy %s", filename);
401
                perror("identifyfile2");
402
        }
403
 
404 28 lampret
        fclose(inputfs);
405
 
406 2 cvs
        return;
407
}
408
 
409 67 lampret
 
410
/* Loads file to memory starting at address startaddr and returns freemem. */
411
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
412 2 cvs
{
413 123 markom
  int breakpoint = 0;
414
 
415 67 lampret
        transl_error = 0;
416
        transl_table = virtphy_transl;
417
        freemem = startaddr;
418
        printf("loadcode: filename %s  startaddr=%x  virtphy_transl=%x", filename, startaddr, virtphy_transl);
419 28 lampret
        identifyfile(filename);
420 67 lampret
        if (transl_error)
421
                return -1;
422
        else
423 123 markom
                return translate(freemem,&breakpoint);
424 2 cvs
}

powered by: WebSVN 2.1.0

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