-
Notifications
You must be signed in to change notification settings - Fork 1
/
jdis.c
180 lines (157 loc) · 3.71 KB
/
jdis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* SPDX-License-Identifier: CC0-1.0
*
* Author: James Jones
*/
#include "jrisc_base.h"
#include "jrisc_ctx.h"
#include "jrisc_ctx_file.h"
#include "jrisc_inst.h"
#include "jrisc_inst_string.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
static void
version(void)
{
printf("Jaguar RISC Disassembler Version %d.%d.%d\n",
JDIS_MAJOR, JDIS_MINOR, JDIS_MICRO);
}
static void
usage(void)
{
version();
printf("\n");
printf("Usage: jdis [-gdamhv] [-o <offset>] [-b <base address>] <JRISC machine code file>\n");
printf("\n");
printf("Options:\n");
printf(" -g: Parse code as Tom/GPU instructions [default].\n");
printf(" -d: Parse code as Jerry/DSP instructions.\n");
printf(" -a: Print address in hex of each disassembled word.\n");
printf(" -m: Print machine code in hex of each disassembled word.\n");
printf(" -o <offset>: Specify offset into file (0x<hex> or <decimal>)\n");
printf(" -b <base address>: Specify the base load address of the code\n");
printf(" -h: Help. Print this text.\n");
printf(" -v: Version. Print the version and exit.\n");
printf("\n");
printf("Offsets and addresses are parsed as hex if they start '0x',\n");
printf(" octal if they start with '0', or decimal otherwise.\n");
}
int
main(int argc, char *argv[])
{
FILE *fp;
struct JRISC_Context *ctx;
struct JRISC_Instruction inst;
enum JRISC_Error err;
const char *fileName = NULL;
enum JRISC_CPU cpu = JRISC_gpu;
uint64_t fileOffset = 0;
uint32_t baseAddress = 0;
bool baseSpecified = false;
uint32_t stringFlags = 0;
int i;
int j;
bool skipParam;
char *end;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
for (j = 1, skipParam = false; !skipParam && argv[i][j]; j++) {
switch (argv[i][j]) {
case 'h':
usage();
exit(0);
case 'v':
version();
exit(0);
case 'g':
cpu = JRISC_gpu;
break;
case 'd':
cpu = JRISC_dsp;
break;
case 'a':
stringFlags |= JRISC_STRINGFLAG_ADDRESS;
break;
case 'm':
stringFlags |= JRISC_STRINGFLAG_MACHINE_CODE;
break;
case 'o':
if ((argv[i][j+1]) || (++i >= argc)) {
usage();
exit(1);
}
errno = 0;
fileOffset = strtoll(argv[i], &end, 0);
if (errno != 0) {
perror("Error parsing file offset");
printf("\n");
usage();
exit(1);
}
if (!argv[i][0] || end[0]) {
printf("Error parsing file offset\n\n");
usage();
exit(1);
}
skipParam = true;
break;
case 'b':
if ((argv[i][j+1]) || (++i >= argc)) {
usage();
exit(1);
}
errno = 0;
baseAddress = strtol(argv[i], &end, 0);
if (errno != 0) {
perror("Error parsing base address");
printf("\n");
usage();
exit(1);
}
if (!argv[i][0] || end[0]) {
printf("Error parsing base address\n\n");
usage();
exit(1);
}
baseSpecified = true;
skipParam = true;
break;
default:
usage();
exit(1);
}
}
} else if (!fileName) {
fileName = argv[i];
} else {
usage();
exit(1);
}
}
if (!baseSpecified) {
baseAddress = (cpu == JRISC_gpu) ? JRISC_GPU_RAM : JRISC_DSP_RAM;
}
if (!fileName) {
usage();
exit(1);
}
fp = fopen(fileName, "rb");
if (!fp) {
fprintf(stderr, "Could not open %s\n", fileName);
exit(1);
}
err = jriscContextFromFile(fp, fileOffset, /* read file */
NULL, 0, /* write file+offset */
baseAddress,
&ctx);
if (err != JRISC_success) {
fprintf(stderr, "Failed to create context\n");
exit(1);
}
while ((err = jriscInstructionRead(ctx, cpu, &inst)) == JRISC_success)
jriscInstructionPrint(&inst, stringFlags);
jriscContextDestroy(ctx);
return 0;
}