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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
#include <err.h>
#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include "generator.h"
#include "lang.h"
#include "machine.h"
#include "parse.h"
#include "prepare.h"
#include "khash.h"
static void help(char *arg0) {
printf("Usage: %s --help\n"
" %s {-I/path/to/include}* [--gst] [--prepare|--preproc|--proc] [--arch <arch>|-32|-64|--32|--64] <filename_in>\n"
" %s {-I/path/to/include}* [--gst] [[--emu <arch>] [--target <arch>]|-32|-64|--32|--64] <filename_in> <filename_reqs> <filename_out>\n"
"\n"
" --prepare Dump all preprocessor tokens (prepare phase)\n"
" --preproc Dump all processor tokens (preprocessor phase)\n"
" --proc Dump all typedefs, declarations and constants (processor phase, default)\n"
" -I Add a path to the list of system includes\n"
"\n"
"Filenames may be preceded by '-f' or '--filename'.\n"
" <filename_in> Parsing file\n"
" <filename_reqs> Reference file (example: wrappedlibc_private.h)\n"
" <filename_out> Output file\n"
"\n"
"You can also specify the target architecture:"
" --arch <arch> Use the architecture <arch>\n"
" --emu <arch> Use the architecture <arch> as emulated architecture\n"
" --target <arch> Use the architecture <arch> as target/running architecture\n"
" -32 --32 Use the x86 architecture as arch/emulated, aarch64 as target\n"
" -64 --64 Use the x86_64 architecture as arch/emulated, aarch64 as target\n"
"\n"
" <arch> is one of 'x86', 'x86_64', 'aarch64'\n"
"\n"
" --gst Mark all structures with a tag starting with '_G' and ending with 'Class' as simple\n",
arg0, arg0, arg0);
}
enum main_state {
MAIN_RUN,
MAIN_PREPARE,
MAIN_PREPROC,
MAIN_PROC,
};
enum bits_state {
BITS_NONE,
BITS_32,
BITS_64,
};
int is_gst = 0;
int main(int argc, char **argv) {
setbuf(stdout, NULL);
if (!setlocale(LC_NUMERIC, "C")) {
log_error_nopos("failed to set LC_NUMERIC to C\n");
return 2;
}
enum main_state ms = MAIN_RUN;
enum bits_state bs = BITS_NONE;
const char *in_file = NULL, *ref_file = NULL, *out_file = NULL;
VECTOR(charp) *paths = vector_new(charp);
const char *archname = NULL, *emuname = NULL, *targetname = NULL;
for (int i = 1; i < argc; ++i) {
int isfile = 0;
if (!strcmp(argv[i], "--help")) {
help(argv[0]);
return 0;
} else if (!strcmp(argv[i], "--prepare")) {
ms = MAIN_PREPARE;
} else if (!strcmp(argv[i], "--preproc")) {
ms = MAIN_PREPROC;
} else if (!strcmp(argv[i], "--proc")) {
ms = MAIN_PROC;
} else if (!strcmp(argv[i], "--gst")) {
is_gst = 1;
} else if (!strcmp(argv[i], "-pthread")) {
// Ignore
} else if (!strcmp(argv[i], "-I") && (i + 1 < argc)) {
if (!vector_push(charp, paths, argv[i + 1])) {
log_memory("failed to add path to buffer\n");
return 2;
}
++i;
} else if ((argv[i][0] == '-') && (argv[i][1] == 'I') && (argv[i][2] != '\0')) {
if (!vector_push(charp, paths, argv[i] + 2)) {
log_memory("failed to add path to buffer\n");
return 2;
}
} else if (!strcmp(argv[i], "--arch")) {
++i;
if (i < argc) {
archname = argv[i];
} else {
log_error_nopos("invalid '--arch' option in last position\n");
help(argv[0]);
return 0;
}
} else if (!strcmp(argv[i], "--emu")) {
++i;
if (i < argc) {
emuname = argv[i];
} else {
log_error_nopos("invalid '--emu' option in last position\n");
help(argv[0]);
return 0;
}
} else if (!strcmp(argv[i], "--target")) {
++i;
if (i < argc) {
targetname = argv[i];
} else {
log_error_nopos("invalid '--target' option in last position\n");
help(argv[0]);
return 0;
}
} else if (!strcmp(argv[i], "-32") || !strcmp(argv[i], "--32")) {
bs = BITS_32;
} else if (!strcmp(argv[i], "-64") || !strcmp(argv[i], "--64")) {
bs = BITS_64;
} else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--filename")) {
++i;
if (i < argc) {
isfile = 1;
} else {
log_error_nopos("invalid '--filename' option in last position\n");
help(argv[0]);
return 0;
}
} else {
isfile = 1;
}
if (isfile) {
if (!in_file) {
in_file = argv[i];
} else if (!ref_file) {
ref_file = argv[i];
} else if (!out_file) {
out_file = argv[i];
} else {
log_error_nopos("too many unknown options considered as file\n");
help(argv[0]);
return 2;
}
}
}
switch (ms) {
case MAIN_PREPARE:
case MAIN_PREPROC:
case MAIN_PROC:
check_proc:
if (!in_file || ref_file || out_file) {
log_error_nopos("too many unknown options/not enough arguments\n");
help(argv[0]);
return 2;
}
if (emuname || targetname) {
log_error_nopos("invalid option '--emu' or '--target' in prepare/preprocessor/processor mode\n");
help(argv[0]);
return 2;
}
if (bs != BITS_NONE) {
if (archname) {
log_error_nopos("invalid option '--arch' with '--32' or '--64' in prepare/preprocessor/processor mode\n");
help(argv[0]);
return 2;
}
archname = (bs == BITS_32) ? "x86" : "x86_64";
}
if (!archname) archname = "x86_64";
break;
case MAIN_RUN:
if (in_file && !ref_file && !out_file) {
ms = MAIN_PROC;
goto check_proc;
}
if (!in_file || !ref_file || !out_file) {
log_error_nopos("too many unknown options/not enough arguments\n");
help(argv[0]);
return 2;
}
if (archname) {
log_error_nopos("invalid option '--arch' in run mode\n");
help(argv[0]);
return 2;
}
if (bs != BITS_NONE) {
if (emuname || targetname) {
log_error_nopos("invalid option '--emu' or '--target' with '--32' or '--64' in run mode\n");
help(argv[0]);
return 2;
}
emuname = (bs == BITS_32) ? "x86" : "x86_64";
targetname = "aarch64";
}
if (!emuname) emuname = "x86_64";
if (!targetname) targetname = "aarch64";
break;
}
if (!init_str2kw()) {
return 2;
}
if (!init_machines(vector_size(charp, paths), (const char*const*)vector_content(charp, paths))) {
vector_del(charp, paths);
del_str2kw();
return 2;
}
vector_del(charp, paths);
FILE *f = fopen(in_file, "r");
if (!f) {
err(2, "Error: failed to open %s", in_file);
del_machines();
del_str2kw();
return 2;
}
switch (ms) {
case MAIN_RUN: {
machine_t *emu = convert_machine_name(emuname);
if (!emu) {
log_error_nopos("invalid emulation architecture '%s'\n", emuname);
del_machines();
del_str2kw();
return 0;
}
machine_t *target = convert_machine_name(targetname);
if (!target) {
log_error_nopos("invalid target architecture '%s'\n", targetname);
del_machines();
del_str2kw();
return 0;
}
file_t *emu_content = parse_file(emu, in_file, f); // Takes ownership of f
if (!emu_content) {
log_error_nopos("failed to parse the file\n");
del_machines();
del_str2kw();
prepare_cleanup();
return 0;
}
f = fopen(in_file, "r");
if (!f) {
err(2, "Error: failed to re-open %s", in_file);
file_del(emu_content);
del_machines();
del_str2kw();
prepare_cleanup();
return 2;
}
file_t *target_content = parse_file(target, in_file, f); // Takes ownership of f
if (!target_content) {
log_error_nopos("failed to parse the file\n");
file_del(emu_content);
del_machines();
del_str2kw();
prepare_cleanup();
return 0;
}
FILE *ref = fopen(ref_file, "r");
if (!ref) {
err(2, "Error: failed to open %s", ref_file);
file_del(emu_content);
file_del(target_content);
del_machines();
del_str2kw();
prepare_cleanup();
return 2;
}
VECTOR(references) *refs = references_from_file(ref_file, ref);
if (!refs) {
file_del(emu_content);
file_del(target_content);
del_machines();
del_str2kw();
prepare_cleanup();
return 2;
}
// vector_for(references, req, refs) request_print(req);
if (target->size_long != emu->size_long) {
if (!solve_references(refs, emu_content->decl_map, target_content->decl_map, emu_content->relaxed_type_conversion)) {
log_warning_nopos("failed to solve all default requests\n");
}
} else {
if (!solve_references_simple(refs, emu_content->decl_map, target_content->decl_map, emu_content->relaxed_type_conversion)) {
log_warning_nopos("failed to solve all default requests\n");
}
}
// vector_for(references, req, refs) request_print(req);
references_print_check(refs);
FILE *out = fopen(out_file, "w");
if (!out) {
err(2, "Error: failed to open %s", ref_file);
file_del(emu_content);
file_del(target_content);
vector_del(references, refs);
del_machines();
del_str2kw();
prepare_cleanup();
return 2;
}
output_from_references(out, refs);
fclose(out);
vector_del(references, refs);
file_del(emu_content);
file_del(target_content);
del_machines();
del_str2kw();
prepare_cleanup();
return 0; }
case MAIN_PROC: {
machine_t *arch = convert_machine_name(archname);
if (!arch) {
log_error_nopos("invalid architecture '%s'\n", archname);
}
file_t *content = parse_file(arch, in_file, f); // Takes ownership of f
if (!content) {
log_error_nopos("failed to parse the file\n");
del_machines();
del_str2kw();
prepare_cleanup();
return 0;
}
// print content
const char *name;
struct_t *st;
string_t *str;
type_t *typ;
declaration_t *decl;
num_constant_t cst;
/* for (enum type_builtin_e i = 0; i < LAST_BUILTIN; ++i) {
printf("Builtin %u: %p, ", i, content->builtins[i]);
type_print(content->builtins[i]);
printf("\n");
} */
kh_foreach(content->struct_map, name, st,
printf("Struct: %s -> %p = ", name, st);
struct_print(st);
printf("\n")
)
kh_foreach_key(content->type_set, typ,
printf("Type: %p = ", typ);
type_print(typ);
printf("\n")
)
kh_foreach(content->relaxed_type_conversion, typ, str,
printf("Type conversion: %p -> %s\n", typ, string_content(str));
)
kh_foreach(content->type_map, name, typ,
printf("Typedef: %s -> %p = ", name, typ);
type_print(typ);
printf("\n")
)
kh_foreach(content->enum_map, name, typ,
printf("Enum: %s -> %p = ", name, typ);
type_print(typ);
printf("\n")
)
kh_foreach(content->const_map, name, cst,
printf("Constant: %s -> ", name);
switch (cst.typ) {
case NCT_FLOAT: printf("%ff", cst.val.f); break;
case NCT_DOUBLE: printf("%f", cst.val.d); break;
case NCT_LDOUBLE: printf("%Lfl", cst.val.l); break;
case NCT_INT32: printf("%d", cst.val.i32); break;
case NCT_UINT32: printf("%uu", cst.val.u32); break;
case NCT_INT64: printf("%ldll", cst.val.i64); break;
case NCT_UINT64: printf("%lullu", cst.val.u64); break;
}
printf("\n")
)
kh_foreach(content->decl_map, name, decl,
printf("Declaration: %s -> %p = %u/%p: ", name, decl, decl->storage, decl->typ);
type_print(decl->typ);
printf("\n")
)
file_del(content);
del_machines();
del_str2kw();
prepare_cleanup();
return 0; }
case MAIN_PREPARE:
// Ignored --arch, since this phase does not depend on the architecture
dump_prepare(in_file, f); // Takes ownership of f
del_machines();
del_str2kw();
prepare_cleanup();
return 0;
case MAIN_PREPROC: {
machine_t *arch = convert_machine_name(archname);
if (!arch) {
log_error_nopos("invalid architecture '%s'\n", archname);
}
dump_preproc(arch, in_file, f); // Takes ownership of f
del_machines();
del_str2kw();
prepare_cleanup();
return 0; }
}
log_internal_nopos("failed to run mode %u\n", ms);
del_machines();
del_str2kw();
prepare_cleanup();
return 2;
}
|