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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [binutils/] [dof/] [dof.c] - Blame information for rev 15

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

Line No. Rev Author Line
1 7 hellwig
/*
2
 * dof.c -- dump object file
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
#include "../include/a.out.h"
12
 
13
 
14
#define MSB     ((unsigned int) 1 << (sizeof(unsigned int) * 8 - 1))
15
 
16
 
17
/**************************************************************/
18
 
19
 
20
FILE *inFile;
21
ExecHeader execHeader;
22
char *segmentName[4] = { "ABS", "CODE", "DATA", "BSS" };
23
char *methodName[5] = { "H16", "L16", "R16", "R26", "W32" };
24
 
25
 
26
/**************************************************************/
27
 
28
 
29
void error(char *fmt, ...) {
30
  va_list ap;
31
 
32
  va_start(ap, fmt);
33
  printf("Error: ");
34
  vprintf(fmt, ap);
35
  printf("\n");
36
  va_end(ap);
37
  exit(1);
38
}
39
 
40
 
41
/**************************************************************/
42
 
43
 
44
unsigned int read4FromEco(unsigned char *p) {
45
  return (unsigned int) p[0] << 24 |
46
         (unsigned int) p[1] << 16 |
47
         (unsigned int) p[2] <<  8 |
48
         (unsigned int) p[3] <<  0;
49
}
50
 
51
 
52
void write4ToEco(unsigned char *p, unsigned int data) {
53
  p[0] = data >> 24;
54
  p[1] = data >> 16;
55
  p[2] = data >>  8;
56
  p[3] = data >>  0;
57
}
58
 
59
 
60
void conv4FromEcoToNative(unsigned char *p) {
61
  unsigned int data;
62
 
63
  data = read4FromEco(p);
64
  * (unsigned int *) p = data;
65
}
66
 
67
 
68
void conv4FromNativeToEco(unsigned char *p) {
69
  unsigned int data;
70
 
71
  data = * (unsigned int *) p;
72
  write4ToEco(p, data);
73
}
74
 
75
 
76
/**************************************************************/
77
 
78
 
79
#define CODE_START      (sizeof(ExecHeader))
80
#define DATA_START      (CODE_START + execHeader.csize)
81
#define CRELOC_START    (DATA_START + execHeader.dsize)
82
#define DRELOC_START    (CRELOC_START + execHeader.crsize)
83
#define SYMTBL_START    (DRELOC_START + execHeader.drsize)
84
#define STRING_START    (SYMTBL_START + execHeader.symsize)
85
 
86
 
87
void dumpHeader(void) {
88
  if (fseek(inFile, 0, SEEK_SET) < 0) {
89
    error("cannot seek to exec header");
90
  }
91
  if (fread(&execHeader, sizeof(ExecHeader), 1, inFile) != 1) {
92
    error("cannot read exec header");
93
  }
94
  conv4FromEcoToNative((unsigned char *) &execHeader.magic);
95
  conv4FromEcoToNative((unsigned char *) &execHeader.csize);
96
  conv4FromEcoToNative((unsigned char *) &execHeader.dsize);
97
  conv4FromEcoToNative((unsigned char *) &execHeader.bsize);
98
  conv4FromEcoToNative((unsigned char *) &execHeader.crsize);
99
  conv4FromEcoToNative((unsigned char *) &execHeader.drsize);
100
  conv4FromEcoToNative((unsigned char *) &execHeader.symsize);
101
  conv4FromEcoToNative((unsigned char *) &execHeader.strsize);
102
  if (execHeader.magic != EXEC_MAGIC) {
103
    error("wrong magic number in exec header");
104
  }
105
  printf("Header\n");
106
  printf("    size of code         : %8u bytes\n", execHeader.csize);
107
  printf("    size of data         : %8u bytes\n", execHeader.dsize);
108
  printf("    size of bss          : %8u bytes\n", execHeader.bsize);
109
  printf("    size of code relocs  : %8u bytes\n", execHeader.crsize);
110
  printf("    size of data relocs  : %8u bytes\n", execHeader.drsize);
111
  printf("    size of symbol table : %8u bytes\n", execHeader.symsize);
112
  printf("    size of string space : %8u bytes\n", execHeader.strsize);
113
}
114
 
115
 
116
void dumpBytes(unsigned int totalSize) {
117
  unsigned int currSize;
118
  unsigned char line[16];
119
  int n, i;
120
  unsigned char c;
121
 
122
  currSize = 0;
123
  while (currSize < totalSize) {
124
    if (totalSize - currSize >= 16) {
125
      n = 16;
126
    } else {
127
      n = totalSize - currSize;
128
    }
129
    for (i = 0; i < n; i++) {
130
      line[i] = fgetc(inFile);
131
    }
132
    printf("%08X:  ", currSize);
133
    for (i = 0; i < 16; i++) {
134
      if (i < n) {
135
        c = line[i];
136
        printf("%02X", c);
137
      } else {
138
        printf("  ");
139
      }
140
      printf(" ");
141
    }
142
    printf("  ");
143
    for (i = 0; i < 16; i++) {
144
      if (i < n) {
145
        c = line[i];
146
        if (c >= 32 && c <= 126) {
147
          printf("%c", c);
148
        } else {
149
          printf(".");
150
        }
151
      } else {
152
        printf(" ");
153
      }
154
    }
155
    printf("\n");
156
    currSize += n;
157
  }
158
}
159
 
160
 
161
void dumpCode(void) {
162
  if (fseek(inFile, CODE_START, SEEK_SET) < 0) {
163
    error("cannot seek to code section");
164
  }
165
  printf("\nCode Segment\n");
166
  dumpBytes(execHeader.csize);
167
}
168
 
169
 
170
void dumpData(void) {
171
  if (fseek(inFile, DATA_START, SEEK_SET) < 0) {
172
    error("cannot seek to data section");
173
  }
174
  printf("\nData Segment\n");
175
  dumpBytes(execHeader.dsize);
176
}
177
 
178
 
179
void dumpRelocs(unsigned int totalSize) {
180
  unsigned int currSize;
181
  int n;
182
  RelocRecord relRec;
183
 
184
  currSize = 0;
185
  n = 0;
186
  while (currSize < totalSize) {
187
    if (fread(&relRec, sizeof(RelocRecord), 1, inFile) != 1) {
188
      error("cannot read relocation record");
189
    }
190
    conv4FromEcoToNative((unsigned char *) &relRec.offset);
191
    conv4FromEcoToNative((unsigned char *) &relRec.method);
192
    conv4FromEcoToNative((unsigned char *) &relRec.value);
193
    conv4FromEcoToNative((unsigned char *) &relRec.base);
194
    printf("    %d:\n", n);
195
    printf("        offset  = 0x%08X\n", relRec.offset);
196
    if (relRec.method < 0 || relRec.method > 4) {
197
      error("illegal relocation method");
198
    }
199
    printf("        method  = %s\n", methodName[relRec.method]);
200
    printf("        value   = 0x%08X\n", relRec.value);
201
    if (relRec.base & MSB) {
202
      printf("        base    = symbol # %d\n", relRec.base & ~MSB);
203
    } else {
204
      if (relRec.base < 0 || relRec.base > 3) {
205
        error("base contains an illegal segment number");
206
      }
207
      printf("        base    = %s\n", segmentName[relRec.base]);
208
    }
209
    currSize += sizeof(RelocRecord);
210
    n++;
211
  }
212
}
213
 
214
 
215
void dumpCodeRelocs(void) {
216
  if (fseek(inFile, CRELOC_START, SEEK_SET) < 0) {
217
    error("cannot seek to code relocation section");
218
  }
219
  printf("\nCode Relocation Records\n");
220
  dumpRelocs(execHeader.crsize);
221
}
222
 
223
 
224
void dumpDataRelocs(void) {
225
  if (fseek(inFile, DRELOC_START, SEEK_SET) < 0) {
226
    error("cannot seek to data relocation section");
227
  }
228
  printf("\nData Relocation Records\n");
229
  dumpRelocs(execHeader.drsize);
230
}
231
 
232
 
233
void dumpString(unsigned int offset) {
234
  long pos;
235
  int c;
236
 
237
  pos = ftell(inFile);
238
  if (fseek(inFile, STRING_START + offset, SEEK_SET) < 0) {
239
    error("cannot seek to string");
240
  }
241
  while (1) {
242
    c = fgetc(inFile);
243
    if (c == EOF) {
244
      error("unexpected end of file");
245
    }
246
    if (c == 0) {
247
      break;
248
    }
249
    fputc(c, stdout);
250
  }
251
  fseek(inFile, pos, SEEK_SET);
252
}
253
 
254
 
255
void dumpSymbolTable(void) {
256
  unsigned int currSize;
257
  int n;
258
  SymbolRecord symRec;
259
 
260
  if (fseek(inFile, SYMTBL_START, SEEK_SET) < 0) {
261
    error("cannot seek to symbol table section");
262
  }
263
  printf("\nSymbol Table Records\n");
264
  currSize = 0;
265
  n = 0;
266
  while (currSize < execHeader.symsize) {
267
    if (fread(&symRec, sizeof(SymbolRecord), 1, inFile) != 1) {
268
      error("cannot read symbol record");
269
    }
270
    conv4FromEcoToNative((unsigned char *) &symRec.name);
271
    conv4FromEcoToNative((unsigned char *) &symRec.type);
272
    conv4FromEcoToNative((unsigned char *) &symRec.value);
273
    printf("    %d:\n", n);
274
    printf("        name    = ");
275
    dumpString(symRec.name);
276
    printf("\n");
277
    if (symRec.type & MSB) {
278
      printf("        --- undefined ---\n");
279
    } else {
280
      if (symRec.type < 0 || symRec.type > 3) {
281
        error("type contains an illegal segment number");
282
      }
283
      printf("        segment = %s\n", segmentName[symRec.type]);
284
      printf("        value   = 0x%08X\n", symRec.value);
285
    }
286
    currSize += sizeof(SymbolRecord);
287
    n++;
288
  }
289
}
290
 
291
 
292
/**************************************************************/
293
 
294
 
295
void usage(char *myself) {
296
  printf("Usage: %s\n", myself);
297
  printf("         [-c]             dump code\n");
298
  printf("         [-d]             dump data\n");
299
  printf("         [-x]             dump code relocations\n");
300
  printf("         [-y]             dump data relocations\n");
301
  printf("         [-s]             dump symbol table\n");
302
  printf("         [-a]             dump all\n");
303
  printf("         file             object file to be dumped\n");
304
  exit(1);
305
}
306
 
307
 
308
int main(int argc, char *argv[]) {
309
  int i;
310
  char *argp;
311
  int optionCode;
312
  int optionData;
313
  int optionCodeRelocs;
314
  int optionDataRelocs;
315
  int optionSymbolTable;
316
  char *inName;
317
 
318
  optionCode = 0;
319
  optionData = 0;
320
  optionCodeRelocs = 0;
321
  optionDataRelocs = 0;
322
  optionSymbolTable = 0;
323
  inName = NULL;
324
  for (i = 1; i < argc; i++) {
325
    argp = argv[i];
326
    if (*argp == '-') {
327
      argp++;
328
      switch (*argp) {
329
        case 'c':
330
          optionCode = 1;
331
          break;
332
        case 'd':
333
          optionData = 1;
334
          break;
335
        case 'x':
336
          optionCodeRelocs = 1;
337
          break;
338
        case 'y':
339
          optionDataRelocs = 1;
340
          break;
341
        case 's':
342
          optionSymbolTable = 1;
343
          break;
344
        case 'a':
345
          optionCode = 1;
346
          optionData = 1;
347
          optionCodeRelocs = 1;
348
          optionDataRelocs = 1;
349
          optionSymbolTable = 1;
350
          break;
351
        default:
352
          usage(argv[0]);
353
      }
354
    } else {
355
      if (inName != NULL) {
356
        usage(argv[0]);
357
      }
358
      inName = argp;
359
    }
360
  }
361
  if (inName == NULL) {
362
    usage(argv[0]);
363
  }
364
  inFile = fopen(inName, "r");
365
  if (inFile == NULL) {
366
    error("cannot open input file '%s'", inName);
367
  }
368
  dumpHeader();
369
  if (optionCode) {
370
    dumpCode();
371
  }
372
  if (optionData) {
373
    dumpData();
374
  }
375
  if (optionCodeRelocs) {
376
    dumpCodeRelocs();
377
  }
378
  if (optionDataRelocs) {
379
    dumpDataRelocs();
380
  }
381
  if (optionSymbolTable) {
382
    dumpSymbolTable();
383
  }
384
  fclose(inFile);
385
  return 0;
386
}

powered by: WebSVN 2.1.0

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