comparison dspanal/char2coff.c @ 308:f8344bc4fd61

dspanal: char2coff utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 28 Oct 2019 05:20:50 +0000
parents
children
comparison
equal deleted inserted replaced
307:8e816bba2ff7 308:f8344bc4fd61
1 /*
2 * This program will convert C char array files into COFF
3 * for disassembly analysis.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include "coffout.h"
11
12 extern unsigned read_char_file();
13
14 static u_char binbuffer[300000];
15 static unsigned array_size;
16 static u_char *array_ptr, *array_end;
17
18 #define MAX_SECTIONS 256
19
20 static struct coff_section sections[MAX_SECTIONS];
21 static unsigned nsections;
22
23 static void
24 add_section(addr, size, data)
25 uint32_t addr, size;
26 u_char *data;
27 {
28 struct coff_section *sec;
29
30 if (nsections >= MAX_SECTIONS) {
31 fprintf(stderr, "error: too many sections\n");
32 exit(1);
33 }
34 sec = sections + nsections;
35 sec->addr = addr;
36 sec->size = size;
37 sec->data = data;
38 sec->flags = 0x0020;
39 sec->mempage = 0x00;
40 nsections++;
41 }
42
43 static uint32_t
44 get_longword()
45 {
46 uint32_t datum;
47
48 if (array_ptr + 4 > array_end) {
49 fprintf(stderr, "error: parse spills past end of array\n");
50 exit(1);
51 }
52 datum = (array_ptr[3] << 24) | (array_ptr[2] << 16) |
53 (array_ptr[1] << 8) | array_ptr[0];
54 array_ptr += 4;
55 return datum;
56 }
57
58 main(argc, argv)
59 char **argv;
60 {
61 uint32_t addr, size;
62
63 if (argc != 3) {
64 fprintf(stderr, "usage: %s char-array-file coff-output-file\n",
65 argv[0]);
66 exit(1);
67 }
68 array_size = read_char_file(argv[1], binbuffer, sizeof binbuffer);
69 array_ptr = binbuffer;
70 array_end = array_ptr + array_size;
71 if (get_longword() != 0) {
72 fprintf(stderr, "error: initial tag 0 is missing\n");
73 exit(1);
74 }
75 if (get_longword() != 3) {
76 fprintf(stderr, "error: initial vers 3 is missing\n");
77 exit(1);
78 }
79 for (;;) {
80 size = get_longword();
81 if (size > 0x10000) {
82 fprintf(stderr, "error: section size unreasonable\n");
83 exit(1);
84 }
85 addr = get_longword();
86 if (!size)
87 break;
88 add_section(addr, size, array_ptr);
89 array_ptr += size << 1;
90 }
91 emit_coff_output(argv[2], sections, nsections);
92 exit(0);
93 }