annotate dspanal/char2coff.c @ 352:bc5ddad215f2

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