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

Subversion Repositories or1k

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 cvs
/* parce.c -- Architecture independent load and parsing of assembly
2
   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 "parse.h"
26
#include "abstract.h"
27 18 lampret
#include "arch.h"
28 67 lampret
#include "dmmu.h"
29 28 lampret
#include "coff.h"
30 134 markom
#include "opcode/or32.h"
31 2 cvs
 
32
#define MAXLINE_LEN     18000
33
 
34 28 lampret
extern char *disassembled;
35
 
36 2 cvs
/* Unused mem memory marker. It is used when allocating program and data memory
37
   during parsing */
38
unsigned int freemem;
39
 
40 67 lampret
/* Translation table provided by microkernel. Only used if simulating microkernel. */
41
static unsigned long transl_table;
42
 
43
/* Used to signal whether during loading of programs a translation fault occured. */
44
static unsigned long transl_error;
45
 
46
 
47 2 cvs
int nonempty(char *line)
48
{
49
        int i;
50
 
51
        for(i = 0; i < strlen(line); i++)
52
                if (!isspace(line[i]))
53
                        return(1);
54
        return(0);
55
}
56
 
57
int nondigit(char *line)
58
{
59
        int i;
60
 
61
        for(i = 0; i < strlen(line); i++)
62
                if (!isdigit(line[i]))
63
                        return(1);
64
        return(0);
65
}
66
 
67
char *strtoken(char *in, char *out, int which)
68
{
69
        char    *super;
70
        char    *sub;
71
        char    *newline;
72
 
73
        super = strdup(in);
74
        sub = strtok(super, " \t");
75
        while (sub && --which)
76
                sub = strtok(NULL, " \t");
77
        if (sub && !which) {
78
                if ((newline = strchr(sub, '\n')))
79
                        newline[0] = '\0';
80
                strcpy(out, sub);
81
        } else
82
                out[0] = '\0';
83
        free(super);
84
        if ((newline = strchr(out, '\r')))      /* get rid of CR */
85
                newline[0] = '\0';
86
        return(out);
87
}
88
 
89 67 lampret
/* Used only by the simulator loader to translate logical addresses int ophysical.
90
   If loadcode() is called with valid virtphy_transl pointer to a table of
91
   translations then translate() performs translation otherwise phy address is
92
   equal to logical. */
93 123 markom
static unsigned int translate(unsigned int laddr,int* breakpoint)
94 67 lampret
{
95
        int i;
96
 
97
        /* No translation (i.e. when loading kernel into simulator)
98
/*      printf("transl_table=%x  laddr=%x\n", transl_table, laddr);
99
        printf("laddr=%x\n", laddr);*/
100
        if (transl_table == 0)
101
                return laddr;
102
 
103
        /* Try to find our translation in the table. */
104
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
105 123 markom
                if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i,breakpoint)) {
106 67 lampret
                        setsim_mem32(transl_table + i + 8, -2); /* Page modified */
107 123 markom
                        printf("found paddr=%x\n", evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (PAGE_SIZE - 1)));
108
                        return (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (unsigned long)(PAGE_SIZE - 1));
109 67 lampret
                }
110
 
111
        /* Allocate new phy page for us. */
112
        for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
113 123 markom
                if (evalsim_mem32(transl_table + i + 8,breakpoint) == 0) {
114 67 lampret
                        setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
115
                        setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
116
                        setsim_mem32(transl_table + i + 8, -2); /* Page modified */
117 123 markom
                        printf("newly allocated ppn=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint));
118 67 lampret
                        printf("newly allocated .ppn=%x\n", (unsigned long)transl_table + i + 4);
119
                        printf("newly allocated ofs=%x\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
120 123 markom
                        printf("newly allocated paddr=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (PAGE_SIZE - 1)));
121
                        return (unsigned long)evalsim_mem32(transl_table + i + 4,breakpoint) | (laddr & (unsigned long)(PAGE_SIZE - 1));
122 67 lampret
                }
123
        /* If we come this far then all phy memory is used and we can't find our page
124
           nor allocate new page. */
125
        transl_error = 1;
126
 
127
        printf("can't translate\n", laddr);
128
        exit(1);
129
        return -1;
130
}
131
 
132 123 markom
void adddatastr(char *str,int* breakpoint)
133 2 cvs
{
134
        if (str)
135
                str++;
136
        else
137
                return;
138
 
139 123 markom
        for(; *str && *str != '\"'; str++, translate(freemem++,breakpoint))
140 2 cvs
                if (*str == '\\')
141
                        switch (*++str) {
142 123 markom
                                case 'n': mem[translate(freemem,breakpoint)].data = '\n';
143 2 cvs
                                        break;
144 123 markom
                                case 't': mem[translate(freemem,breakpoint)].data = '\t';
145 2 cvs
                                        break;
146 123 markom
                                case 'r': mem[translate(freemem,breakpoint)].data = '\r';
147 2 cvs
                                        break;
148 123 markom
                                case '0': mem[translate(freemem,breakpoint)].data = '\0';
149 2 cvs
                                        break;
150
                                default: break;
151
                        }
152
                else
153 123 markom
                        mem[translate(freemem,breakpoint)].data = *str;
154 2 cvs
}
155
 
156 123 markom
/* Modified by CZ 26/05/01 */
157
/* Added code for new mode operation */
158
void adddataword(char *item,int* breakpoint)
159 2 cvs
{
160 26 lampret
        unsigned long num;
161 2 cvs
 
162 26 lampret
        if (isdigit(*item))
163 36 lampret
                num = strtoul(item, NULL, 0);
164 26 lampret
        else
165
                num = eval_label(item);
166
 
167 123 markom
        debug("adddataword: [0x%x] <= %x\n", translate(freemem,breakpoint), num);
168
        mem[translate(freemem,breakpoint)].data = (char) (num >> 24);
169
        mem[translate(freemem + 1,breakpoint)].data = (char) (num >> 16);
170
        mem[translate(freemem + 2,breakpoint)].data = (char) (num >> 8);
171
        mem[translate(freemem + 3,breakpoint)].data = (char) (num);
172 67 lampret
 
173 123 markom
        if(!GlobalMode)
174
          freemem += 4;
175 2 cvs
}
176
 
177 123 markom
void adddatahalf(char *item,int* breakpoint)
178 2 cvs
{
179 26 lampret
        unsigned long num;
180 2 cvs
 
181 26 lampret
        if (isdigit(*item))
182 36 lampret
                num = strtoul(item, NULL, 0);
183 26 lampret
        else
184
                num = eval_label(item);
185
 
186 123 markom
        mem[translate(freemem,breakpoint)].data = (char) (num >> 8);
187
        mem[translate(freemem + 1,breakpoint)].data = (char) (num);
188 26 lampret
 
189 2 cvs
        freemem += 2;
190
}
191
 
192 123 markom
void adddatabyte(char *item,int* breakpoint)
193 2 cvs
{
194 26 lampret
        unsigned long num;
195 2 cvs
 
196 26 lampret
        if (isdigit(*item))
197 36 lampret
                num = strtoul(item, NULL, 0);
198 26 lampret
        else
199
                num = eval_label(item);
200
 
201 123 markom
        mem[translate(freemem,breakpoint)].data = (char) (num);
202 26 lampret
 
203 2 cvs
        freemem++;
204
}
205
 
206
void adddataspace(char *num)
207
{
208
        freemem += atol(num);
209
}
210
 
211 123 markom
void addlabel(char *label, unsigned long freemem,int* breakpoint)
212 2 cvs
{
213 28 lampret
        struct label_entry **tmp;
214 2 cvs
 
215 123 markom
        debug("Adding label %s at 0x%x\n", label, translate(freemem,breakpoint));
216
        tmp = &mem[translate(freemem,breakpoint)].label;
217 28 lampret
        for (; *tmp; tmp = &((*tmp)->next));
218
        *tmp = malloc(sizeof(**tmp));
219
        (*tmp)->name = malloc(strlen(label)+1);
220
        strcpy((*tmp)->name, label);
221
        (*tmp)->next = NULL;
222
 
223 2 cvs
        return;
224
}
225
 
226 28 lampret
char null_str[1] = "\0";
227
 
228 123 markom
/* Modified by CZ 26/05/01 */
229
/* Replaced several calls to translate(freemem) with vaddr */
230
/* Added new mode execution code */
231
/* Changed parameters so address can be passed as argument */
232
void addprogram(char *insn, char *operands,unsigned int address,int* breakpoint)
233 2 cvs
{
234 18 lampret
        int h_insn_is_word_flag=0;
235
        char insn_first2_char[3];
236 123 markom
        int vaddr = GlobalMode ? translate(address,breakpoint) : translate(freemem,breakpoint);
237 18 lampret
 
238 28 lampret
        debug("addprogram 1\n");
239 123 markom
        if (!mem[vaddr].insn) {
240
          mem[vaddr].insn = (struct insn_entry *)malloc (sizeof (struct insn_entry));
241
          mem[vaddr].insn->insn_index = -1;
242
          mem[vaddr].insn->op1 = null_str;
243
          mem[vaddr].insn->op2 = null_str;
244
          mem[vaddr].insn->op3 = null_str;
245
          mem[vaddr].insn->op4 = null_str;
246
        } else if(!GlobalMode) {  /* Old mode */
247 28 lampret
                printf("internal error: reloading the same location\n");
248
                exit(1);
249 123 markom
        } else /* New mode */
250
          {
251
            if(mem[vaddr].insn->op1 != null_str) free(mem[vaddr].insn->op1);
252
            if(mem[vaddr].insn->op2 != null_str) free(mem[vaddr].insn->op2);
253
            if(mem[vaddr].insn->op3 != null_str) free(mem[vaddr].insn->op3);
254
            if(mem[vaddr].insn->op4 != null_str) free(mem[vaddr].insn->op4);
255
            mem[vaddr].insn->insn_index = -1;
256
            mem[vaddr].insn->op1 = null_str;
257
            mem[vaddr].insn->op2 = null_str;
258
            mem[vaddr].insn->op3 = null_str;
259
            mem[vaddr].insn->op4 = null_str;
260
          }
261
 
262 28 lampret
        debug("addprogram 2\n");
263
 
264
#ifdef  OR16
265
 
266 123 markom
        printf("half:%s:\n", insn);
267 18 lampret
        insn_first2_char[0]=insn[0];
268
        insn_first2_char[1]=insn[1];
269
        insn_first2_char[2]='\0';
270 28 lampret
        debug("addprogram 3\n");
271 2 cvs
 
272 18 lampret
        if(strcmp("h.", insn_first2_char) == 0) {
273
                if(strcmp("h.load32u", insn) == 0 ||
274
                   strcmp("h.load16u", insn) == 0 ||
275
                   strcmp("h.load8u", insn) == 0 ||
276
                   strcmp("h.stor32", insn) == 0 ||
277
                   strcmp("h.stor16", insn) == 0 ||
278
                   strcmp("h.stor8", insn) == 0 ||
279
                   strcmp("h.jal", insn) == 0 ||
280 22 lampret
                   /* strcmp("h.mtsr", insn) == 0 ||
281
                   strcmp("h.mfsr", insn) == 0 || */
282 18 lampret
                   strcmp("h.movi16ze", insn) == 0 ||
283
                   strcmp("h.immhi16u", insn) == 0 ||
284
                   strcmp("h.addi16s", insn) == 0 ||
285
                   strcmp("h.subi16s", insn) == 0 ||
286
                   strcmp("h.xori16", insn) == 0 ||
287
                   strcmp("h.ori16", insn) == 0 ||
288
                   strcmp("h.andi16", insn) == 0
289
                  )
290
                        h_insn_is_word_flag = 2; /* h.xxx insn AND occupy 4 bytes */
291
                else
292
                        h_insn_is_word_flag = 1; /* h.xxx insn AND occupy 2 bytes */
293
        }
294
        else {
295
                        h_insn_is_word_flag = 0; /* not h.xxx insn */
296
        }
297
#else
298 28 lampret
        debug("addprogram 4\n");
299
        debug("addprogram 5\n");
300 18 lampret
#endif
301
 
302 123 markom
        /* MM: added instruction index */
303
        mem[vaddr].insn->insn_index = insn_index (insn);
304 2 cvs
        /* op1 */
305 28 lampret
        if (*operands) {
306 123 markom
                mem[vaddr].insn->op1 = malloc(strlen(operands)+1);
307
                strcpy(mem[vaddr].insn->op1, operands);
308 28 lampret
        }
309
 
310
        debug("addprogram 6\n");
311
        debug("operands:%s\n", operands);
312
        if (strstr(operands, OPERAND_DELIM)) {
313
                debug("addprogram 6a\n");
314 123 markom
                operands = strstr(mem[vaddr].insn->op1, OPERAND_DELIM);
315 2 cvs
                *operands = '\0';
316
                operands++;
317
        } else {
318 28 lampret
                debug("addprogram 6b\n");
319 123 markom
          if(!GlobalMode)
320
            {
321 28 lampret
#ifdef OR16
322 18 lampret
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
323
#else
324 2 cvs
                freemem += 4;
325 18 lampret
#endif
326 123 markom
            }
327 2 cvs
                return;
328
        }
329
 
330 28 lampret
        debug("addprogram 7\n");
331 2 cvs
        /* op2 */
332 28 lampret
        if (*operands) {
333 123 markom
                mem[vaddr].insn->op2 = malloc(strlen(operands)+1);
334
                strcpy(mem[vaddr].insn->op2, operands);
335 28 lampret
        }
336
        if (strstr(operands, OPERAND_DELIM)) {
337 123 markom
                operands = strstr(mem[vaddr].insn->op2, OPERAND_DELIM);
338 2 cvs
                *operands = '\0';
339
                operands++;
340
        } else {
341 123 markom
          if(!GlobalMode)
342
            {
343 28 lampret
#ifdef OR16
344 18 lampret
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
345
#else
346 2 cvs
                freemem += 4;
347 18 lampret
#endif
348 123 markom
            }
349
 
350 2 cvs
                return;
351
        }
352
 
353 28 lampret
        debug("addprogram 8\n");
354 2 cvs
        /* op3 */
355 28 lampret
        if (*operands) {
356 123 markom
                mem[vaddr].insn->op3 = malloc(strlen(operands)+1);
357
                strcpy(mem[vaddr].insn->op3, operands);
358 28 lampret
        }
359
        if (strstr(operands, OPERAND_DELIM)) {
360 123 markom
                operands = strstr(mem[vaddr].insn->op3, OPERAND_DELIM);
361 2 cvs
                *operands = '\0';
362
                operands++;
363
        } else {
364 123 markom
          if(!GlobalMode)
365
            {
366 28 lampret
#ifdef OR16
367 18 lampret
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
368
#else
369 2 cvs
                freemem += 4;
370 18 lampret
#endif
371 123 markom
            }
372 2 cvs
                return;
373
        }
374
 
375
        /* op4 */
376 28 lampret
        if (*operands) {
377 123 markom
                mem[vaddr].insn->op4 = malloc(strlen(operands)+1);
378
                strcpy(mem[vaddr].insn->op4, operands);
379 28 lampret
        }
380
        if (strstr(operands, OPERAND_DELIM)) {
381 123 markom
                operands = strstr(mem[vaddr].insn->op4, OPERAND_DELIM);
382 2 cvs
                *operands = '\0';
383
                operands++;
384
        }
385
 
386 123 markom
        if(!GlobalMode)
387
          {
388 28 lampret
#ifdef OR16
389 18 lampret
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
390
#else
391
                freemem += 4;
392
#endif
393 123 markom
          }
394
 
395 2 cvs
        return;
396
}
397
 
398
/* Non-architecture dependent parsing: stripping comments, filling
399
   abstract memory */
400
 
401 123 markom
void parseline(char *inputline,int* breakpoint)
402 2 cvs
{
403
        char item[MAXLINE_LEN];
404
        char item2[MAXLINE_LEN];
405
        int  i = 0;
406
 
407
        /* Strip comments: simply terminate line where
408
           the first comment character appears. */
409
 
410
        debug("PARSING: %s", inputline);
411
        while (inputline[i] != '\0')
412
                if (inputline[i] == COMMENT_CHAR) {
413
                        inputline[i] = '\0';
414
                        break;
415
                } else
416
                        i++;
417
 
418
        /* Get the first item from this line */
419 18 lampret
        strtoken(inputline, item, 1);   /* opcode */
420
        strtoken(inputline, item2, 2);  /* all the remaining one/two/three operands */
421 2 cvs
 
422
        /* Is this item empty? Nothing to process, so return. */
423
        if (strlen(item) == 0)
424
                return;
425
 
426 18 lampret
        /* Is this item a label? If yes, add it to the label table and return immediately. */
427 2 cvs
        if (strstr(item, LABELEND_CHAR)) {
428 28 lampret
                *strstr(item, LABELEND_CHAR) = '\0';
429 123 markom
                addlabel(item, translate(freemem,breakpoint),breakpoint);
430 2 cvs
                return;
431
        }
432
 
433
        /* Is this item a .directive? If yes, check for some supported
434
           and then return (even if unsupported found). */
435
        if (item[0] == DIRECTIVE_CHAR) {
436 28 lampret
                if (strcmp(item, ".align") == 0) {
437 30 lampret
                        int align = strtoul(item2, NULL, 0);
438 123 markom
                        if (!(translate(freemem,breakpoint) % align))
439 28 lampret
                                return;
440
                        freemem &= -align;
441
                        freemem += align;
442 2 cvs
                        return;
443
                } else
444 30 lampret
                if (strcmp(item, ".org") == 0) {
445
                        int addr = strtoul(item2, NULL, 0);
446
                        freemem = addr;
447
                        return;
448
                } else
449 2 cvs
                if (strcmp(item, ".ascii") == 0) {
450 123 markom
                        adddatastr(strstr(inputline, "\""),breakpoint);
451 2 cvs
                        return;
452
                } else
453
                if (strcmp(item, ".word") == 0) {
454 123 markom
                        adddataword(item2,breakpoint);
455 2 cvs
                        return;
456
                } else
457
                if (strcmp(item, ".half") == 0) {
458 123 markom
                        adddatahalf(item2,breakpoint);
459 2 cvs
                        return;
460
                } else
461
                if (strcmp(item, ".byte") == 0) {
462 123 markom
                        adddatabyte(item2,breakpoint);
463 2 cvs
                        return;
464
                } else
465
                if (strcmp(item, ".space") == 0) {
466
                        adddataspace(item2);
467
                        return;
468
                } else  /* .directive but not one of the supported */
469
                        return;
470
        }
471
 
472
        /* This item can only be an instruction. Get all operands
473
           and add everything to mem array but as a program. */
474 123 markom
        debug("%x: ", translate(freemem,breakpoint));
475
        addprogram(item, item2,freemem,breakpoint);
476 2 cvs
 
477
        /* Also do static, single stats. */
478
        addsstats(item, 0, 1);
479
 
480
        return;
481
 
482
}
483
 
484 28 lampret
/* Load big-endian COFF file. At the moment it doesn't load symbols yet. */
485
 
486
void readfile_coff(char *filename, short sections)
487
{
488
        FILE *inputfs;
489
        char inputbuf[4];
490
        unsigned long insn;
491 41 lampret
        signed long sectsize;
492 28 lampret
        COFF_AOUTHDR coffaouthdr;
493
        struct COFF_scnhdr coffscnhdr;
494
        int  len;
495
        char item[MAXLINE_LEN];
496
        char item2[MAXLINE_LEN];
497 41 lampret
        int  firstthree = 0;
498 123 markom
        int breakpoint = 0;
499
 
500 28 lampret
        if (!(inputfs = fopen(filename, "r"))) {
501
                perror("readfile_coff");
502
                exit(1);
503
        }
504
 
505
        if (fseek(inputfs, sizeof(struct COFF_filehdr), SEEK_SET) == -1) {
506
                fclose(inputfs);
507
                perror("readfile_coff");
508
                exit(1);
509
        }
510
 
511
        if (fread(&coffaouthdr, sizeof(coffaouthdr), 1, inputfs) != 1) {
512
                fclose(inputfs);
513
                perror("readfile_coff");
514
                exit(1);
515
        }
516
 
517
        while(sections--) {
518 41 lampret
                long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
519
                                + sizeof(struct COFF_scnhdr) * firstthree;
520
                if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
521
                        fclose(inputfs);
522
                        perror("readfile_coff");
523
                        exit(1);
524
                }
525 28 lampret
                if (fread(&coffscnhdr, sizeof(struct COFF_scnhdr), 1, inputfs) != 1) {
526
                        fclose(inputfs);
527
                        perror("readfile_coff");
528
                        exit(1);
529
                }
530
                printf("Section: %s,", coffscnhdr.s_name);
531 41 lampret
                printf(" vaddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_vaddr));
532
                printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
533
                printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
534
 
535
                sectsize = COFF_LONG_H(coffscnhdr.s_size);
536
                /* A couple of sanity checks. */
537 123 markom
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr),&breakpoint) < MEMORY_START) {
538 41 lampret
                        printf("Section %s starts out of ", coffscnhdr.s_name);
539
                        printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
540
                        exit(1);
541
                }
542 123 markom
                if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize,&breakpoint) >
543 41 lampret
                    MEMORY_START + MEMORY_LEN) {
544
                        printf("Section %s ends out of ", coffscnhdr.s_name);
545
                        printf("memory.\n");
546
                        exit(1);
547
                }
548
                if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
549
                        printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
550
                        exit(1);
551
                }
552
                if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
553
                        printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
554
                        exit(1);
555
                }
556
                if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
557
                        printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
558
                        exit(1);
559
                }
560 28 lampret
 
561 41 lampret
                /* loading section */
562
                freemem = COFF_LONG_H(coffscnhdr.s_vaddr);
563 67 lampret
                debug("Starting to load at 0x%x", freemem);
564 41 lampret
                if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
565
                        fclose(inputfs);
566
                        perror("readfile_coff");
567
                        exit(1);
568 28 lampret
                }
569 41 lampret
                while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
570
                        insn = COFF_LONG_H(inputbuf);
571
                        len = disassemble_insn(insn);
572
                        sprintf(item, "%u", insn);
573 123 markom
                        adddataword(item,&breakpoint);
574 41 lampret
                        freemem -= len;
575
                        if (len == 2) {
576
                                fseek(inputfs, -2, SEEK_CUR);
577
                                debug("readfile_coff: %x 0x%x   ", sectsize, insn >> 16);
578
                        }
579
                        else
580
                                debug("readfile_coff: %x 0x%x   ", sectsize, insn);
581
                        debug("%s\n", disassembled);
582
                        strtoken(disassembled, item, 1); /* opcode */
583
                        strtoken(disassembled, item2, 2); /* all the remaining one/two/three operands */
584 123 markom
                        addprogram(item, item2,freemem,&breakpoint);
585 41 lampret
                        sectsize -= len;
586
                }
587 28 lampret
        }
588 41 lampret
        if (firstthree < 3) {
589
                printf("One or more missing sections. At least");
590
                printf(" three sections expected (.text, .data, .bss).\n");
591
                exit(1);
592
        }
593
        if (firstthree > 3) {
594
                printf("Warning: one or more extra sections. These");
595
                printf(" sections were handled as .data sections.\n");
596
        }
597
 
598 28 lampret
        fclose(inputfs);
599
        printf("Finished loading COFF.\n");
600
        return;
601
}
602
 
603
/* Load symbols from big-endian COFF file. */
604
 
605
void readsyms_coff(char *filename, unsigned long symptr, long syms)
606
{
607
        FILE *inputfs;
608
        struct COFF_syment coffsymhdr;
609 123 markom
        int breakpoint = 0;
610 28 lampret
 
611
        if (!(inputfs = fopen(filename, "r"))) {
612
                perror("readsyms_coff");
613
                exit(1);
614
        }
615
 
616
        if (fseek(inputfs, symptr, SEEK_SET) == -1) {
617
                fclose(inputfs);
618
                perror("readsyms_coff");
619
                exit(1);
620
        }
621
 
622
        while(syms--) {
623
                if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
624
                        fclose(inputfs);
625
                        perror("readsyms_coff");
626
                        exit(1);
627
                }
628 41 lampret
                debug("Symbol: %s,", coffsymhdr.e.e_name);
629
                debug(" val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
630
                debug(" auxs: %c\n", coffsymhdr.e_numaux);
631 42 lampret
                if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
632 123 markom
                        addlabel(coffsymhdr.e.e_name, COFF_LONG_H(coffsymhdr.e_value),&breakpoint);
633 28 lampret
        }
634
 
635
        fclose(inputfs);
636
        printf("Finished loading symbols.\n");
637
        return;
638
}
639
 
640 2 cvs
/* Load file and hand over every line to parse routine. */
641
 
642 28 lampret
void readfile_assembly(char *filename)
643 2 cvs
{
644
        FILE *inputfs;
645
        char  inputbuf[MAXLINE_LEN];
646
        char  *status;
647 123 markom
        int breakpoint = 0;
648
 
649 28 lampret
        if (!(inputfs = fopen(filename, "r"))) {
650
                perror("readfile_assembly");
651
                exit(1);
652
        }
653
 
654
        while ((status = fgets(inputbuf, sizeof(inputbuf), inputfs))) {
655
                if (nonempty(inputbuf))
656 123 markom
                        parseline(inputbuf,&breakpoint);
657 28 lampret
        }
658
        fclose(inputfs);
659
 
660
        return;
661
}
662
 
663
/* Identify file type and call appropriate readfile_X routine. It only
664
handles orX-coff-big executables at the moment. */
665
 
666
void identifyfile(char *filename)
667
{
668
        FILE *inputfs;
669
        struct COFF_filehdr coffhdr;
670
        size_t len;
671
 
672
        if (!(inputfs = fopen(filename, "r"))) {
673 67 lampret
                fprintf(stderr, "xx %s", filename);
674
                perror("identifyfile1");
675
                fflush(stdout);
676
                fflush(stderr);
677 28 lampret
                exit(1);
678
        }
679
 
680
        if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
681
                if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
682
                        unsigned long opthdr_size;
683
                        printf("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
684
                        printf("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
685
                        printf("COFF symptr: 0x%.8x\n", COFF_LONG_H(coffhdr.f_symptr));
686
                        if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
687
                                printf("This COFF is not an executable.\n");
688
                                exit(1);
689
                        }
690
                        opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
691
                        if (opthdr_size != sizeof(COFF_AOUTHDR)) {
692
                                printf("COFF optional header is missing or not recognized.\n");
693
                                printf("COFF f_opthdr: 0x%.2x\n", opthdr_size);
694
                                exit(1);
695
                        }
696
                        fclose(inputfs);
697
                        readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
698
                        readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
699
                        return;
700 2 cvs
                }
701 28 lampret
                else {
702
                        printf("Not COFF, trying to load as assembly.\n");
703
                        fclose(inputfs);
704
                        readfile_assembly(filename);
705
                        return;
706
                }
707 2 cvs
        }
708 67 lampret
        else {
709
                printf("yy %s", filename);
710
                perror("identifyfile2");
711
        }
712
 
713 28 lampret
        fclose(inputfs);
714
 
715 2 cvs
        return;
716
}
717
 
718 67 lampret
 
719
/* Loads file to memory starting at address startaddr and returns freemem. */
720
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
721 2 cvs
{
722 123 markom
  int breakpoint = 0;
723
 
724 67 lampret
        transl_error = 0;
725
        transl_table = virtphy_transl;
726
        freemem = startaddr;
727
        printf("loadcode: filename %s  startaddr=%x  virtphy_transl=%x", filename, startaddr, virtphy_transl);
728 28 lampret
        identifyfile(filename);
729 67 lampret
        if (transl_error)
730
                return -1;
731
        else
732 123 markom
                return translate(freemem,&breakpoint);
733 2 cvs
}

powered by: WebSVN 2.1.0

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