forked from AlexAltea/unicorn.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
645 lines (597 loc) · 24.9 KB
/
build.py
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#!/usr/bin/python
# INFORMATION:
# This scripts compiles the original Unicorn framework to JavaScript
import os
import glob
import shutil
import stat
import sys
EXPORTED_FUNCTIONS = [
'_uc_version',
'_uc_arch_supported',
'_uc_open',
'_uc_close',
'_uc_query',
'_uc_errno',
'_uc_strerror',
'_uc_reg_write',
'_uc_reg_read',
'_uc_reg_write_batch',
'_uc_reg_read_batch',
'_uc_mem_write',
'_uc_mem_read',
'_uc_emu_start',
'_uc_emu_stop',
'_uc_hook_add',
'_uc_hook_del',
'_uc_mem_map',
'_uc_mem_map_ptr',
'_uc_mem_unmap',
'_uc_mem_protect',
'_uc_mem_regions',
'_uc_context_alloc',
'_uc_free',
'_uc_context_save',
'_uc_context_restore',
]
EXPORTED_CONSTANTS = [
'bindings/python/unicorn/arm64_const.py',
'bindings/python/unicorn/arm_const.py',
'bindings/python/unicorn/m68k_const.py',
'bindings/python/unicorn/mips_const.py',
'bindings/python/unicorn/sparc_const.py',
'bindings/python/unicorn/x86_const.py',
'bindings/python/unicorn/unicorn_const.py',
]
# Directories
UNICORN_DIR = os.path.abspath("unicorn")
UNICORN_QEMU_DIR = os.path.join(UNICORN_DIR, "qemu")
ORIGINAL_QEMU_DIR = os.path.abspath("externals/qemu-2.2.1")
def generateConstants():
out = open('src/unicorn-constants.js', 'w')
for path in EXPORTED_CONSTANTS:
path = os.path.join(UNICORN_DIR, path)
with open(path, 'r') as f:
code = f.read()
code = code.replace('\nUC_', '\nuc.')
code = code.replace('#', '//')
out.write(code)
out.close()
#############
# Utilities #
#############
# Replace strings in files
def replace(path, replacements):
pathBak = path + ".bak"
if os.path.exists(pathBak):
return
shutil.move(path, pathBak)
fin = open(pathBak, "rt")
fout = open(path, "wt")
for line in fin:
for string in replacements:
line = line.replace(string, replacements[string])
fout.write(line)
fin.close()
fout.close()
# Insert strings in files after a specific line
def insert(path, match, strings):
pathBak = path + ".bak"
if os.path.exists(pathBak):
return
shutil.move(path, pathBak)
fin = open(pathBak, "rt")
fout = open(path, "wt")
for line in fin:
fout.write(line)
if match.strip() == line.strip():
for string in strings:
fout.write(string + "\n")
fin.close()
fout.close()
# Append strings at the end of the file
def append(path, code):
pathBak = path + ".bak"
if os.path.exists(pathBak):
return
shutil.copy(path, pathBak)
with open(path, 'a') as f:
f.write(code)
# Prepend strings at the beginning of the file
def prepend(path, code):
pathBak = path + ".bak"
if os.path.exists(pathBak):
return
shutil.move(path, pathBak)
fin = open(pathBak, 'r')
fout = open(path, 'w')
fout.write(code)
fout.write(fin.read())
fout.close()
fin.close()
# Copy directory contents to another folder without overwriting files
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
elif not os.path.exists(d):
shutil.copy2(s, d)
############
# Patching #
############
REPLACE_OBJECTS = """
import re, glob, shutil
path = 'qemu/*softmmu/**/*.o'
for d in xrange(5):
for f in glob.glob(path.replace('/**', '/*' * d)):
f = f.replace('\\\\', '/')
m = re.match(r'qemu\/([0-9A-Za-z_]+)\-softmmu.*', f)
shutil.move(f, f[:-2] + '-' + m.group(1) + '.o')
"""
PATCH_UNALIGNED_MEMACCESS = """
#define UNALIGNED_READ16_LE(addr) ( \\
((uint16_t)(*((uint8_t*)(addr) + 0)) << 0) | \\
((uint16_t)(*((uint8_t*)(addr) + 1)) << 8) \\
)
#define UNALIGNED_READ32_LE(addr) ( \\
((uint32_t)(*((uint8_t*)(addr) + 0)) << 0) | \\
((uint32_t)(*((uint8_t*)(addr) + 1)) << 8) | \\
((uint32_t)(*((uint8_t*)(addr) + 2)) << 16) | \\
((uint32_t)(*((uint8_t*)(addr) + 3)) << 24) \\
)
#define UNALIGNED_READ64_LE(addr) ( \\
((uint64_t)(*((uint8_t*)(addr) + 0)) << 0) | \\
((uint64_t)(*((uint8_t*)(addr) + 1)) << 8) | \\
((uint64_t)(*((uint8_t*)(addr) + 2)) << 16) | \\
((uint64_t)(*((uint8_t*)(addr) + 3)) << 24) | \\
((uint64_t)(*((uint8_t*)(addr) + 4)) << 32) | \\
((uint64_t)(*((uint8_t*)(addr) + 5)) << 40) | \\
((uint64_t)(*((uint8_t*)(addr) + 6)) << 48) | \\
((uint64_t)(*((uint8_t*)(addr) + 7)) << 56) \\
)
#define UNALIGNED_WRITE16_LE(addr, value) { \\
*((uint8_t*)(addr) + 0) = ((value) >> 0) & 0xFF; \\
*((uint8_t*)(addr) + 1) = ((value) >> 8) & 0xFF; \\
}
#define UNALIGNED_WRITE32_LE(addr, value) { \\
*((uint8_t*)(addr) + 0) = ((value) >> 0) & 0xFF; \\
*((uint8_t*)(addr) + 1) = ((value) >> 8) & 0xFF; \\
*((uint8_t*)(addr) + 2) = ((value) >> 16) & 0xFF; \\
*((uint8_t*)(addr) + 3) = ((value) >> 24) & 0xFF; \\
}
#define UNALIGNED_WRITE64_LE(addr, value) { \\
*((uint8_t*)(addr) + 0) = ((value) >> 0) & 0xFF; \\
*((uint8_t*)(addr) + 1) = ((value) >> 8) & 0xFF; \\
*((uint8_t*)(addr) + 2) = ((value) >> 16) & 0xFF; \\
*((uint8_t*)(addr) + 3) = ((value) >> 24) & 0xFF; \\
*((uint8_t*)(addr) + 4) = ((value) >> 32) & 0xFF; \\
*((uint8_t*)(addr) + 5) = ((value) >> 40) & 0xFF; \\
*((uint8_t*)(addr) + 6) = ((value) >> 48) & 0xFF; \\
*((uint8_t*)(addr) + 7) = ((value) >> 56) & 0xFF; \\
}
"""
PATCH_HELPER_ADAPTER_PROTO = """
#define GEN_ADAPTER_ARGS \\
uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4, uint32_t a5, \\
uint32_t a6, uint32_t a7, uint32_t a8, uint32_t a9, uint32_t a10
#define GEN_ADAPTER_DECLARE(name) \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS);
"""
PATCH_HELPER_ADAPTER_GEN = """
// Compile-time dispatch
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
#define IIF_0(t, ...) __VA_ARGS__
#define IIF_1(t, ...) t
#define PROBE(x) x, 1
#define CHECK(...) CHECK_N(__VA_ARGS__, 0)
#define CHECK_N(x, n, ...) n
// Void type detection
#define VOID_TYPE_void ()
#define VOID_TYPE_noreturn ()
#define VOID_PROBE(type) VOID_PROBE_PROXY(VOID_TYPE_##type)
#define VOID_PROBE_PROXY(...) VOID_PROBE_PRIMITIVE(__VA_ARGS__)
#define VOID_PROBE_PRIMITIVE(x) VOID_PROBE_COMBINE_ x
#define VOID_PROBE_COMBINE_(...) PROBE(~)
#define IS_VOID(type) CHECK(VOID_PROBE(type))
// Global helper detection
#define GLOB_NAME_uc_tracecode ()
#define GLOB_NAME_div_i32 ()
#define GLOB_NAME_rem_i32 ()
#define GLOB_NAME_divu_i32 ()
#define GLOB_NAME_remu_i32 ()
#define GLOB_NAME_div_i64 ()
#define GLOB_NAME_rem_i64 ()
#define GLOB_NAME_divu_i64 ()
#define GLOB_NAME_remu_i64 ()
#define GLOB_NAME_shl_i64 ()
#define GLOB_NAME_shr_i64 ()
#define GLOB_NAME_sar_i64 ()
#define GLOB_NAME_mulsh_i64 ()
#define GLOB_NAME_muluh_i64 ()
#define GLOB_PROBE(name) GLOB_PROBE_PROXY(GLOB_NAME_##name)
#define GLOB_PROBE_PROXY(...) GLOB_PROBE_PRIMITIVE(__VA_ARGS__)
#define GLOB_PROBE_PRIMITIVE(x) GLOB_PROBE_COMBINE_ x
#define GLOB_PROBE_COMBINE_(...) PROBE(~)
#define IS_GLOB(name) CHECK(GLOB_PROBE(name))
// Arguments
#define A1 (a1 | ((uint64_t)a2 << 32))
#define A2 (a3 | ((uint64_t)a4 << 32))
#define A3 (a5 | ((uint64_t)a6 << 32))
#define A4 (a7 | ((uint64_t)a8 << 32))
#define A5 (a9 | ((uint64_t)a10 << 32))
#define GEN_ADAPTER_ARGS \\
uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4, uint32_t a5, \\
uint32_t a6, uint32_t a7, uint32_t a8, uint32_t a9, uint32_t a10
// Adapter definition
#define GEN_ADAPTER_0_VOID(name) \\
HELPER(name)(); return 0;
#define GEN_ADAPTER_0_NONVOID(name) \\
return HELPER(name)();
#define GEN_ADAPTER_0_DEFINE(name, ret) \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS) { \\
IIF(IS_VOID(ret)) (GEN_ADAPTER_0_VOID(name), GEN_ADAPTER_0_NONVOID(name)) \\
}
#define GEN_ADAPTER_1_VOID(name, t1) \\
HELPER(name)((dh_ctype(t1))A1); return 0;
#define GEN_ADAPTER_1_NONVOID(name, t1) \\
return HELPER(name)((dh_ctype(t1))A1);
#define GEN_ADAPTER_1_DEFINE(name, ret, t1) \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS) { \\
IIF(IS_VOID(ret)) (GEN_ADAPTER_1_VOID(name, t1), GEN_ADAPTER_1_NONVOID(name, t1)) \\
}
#define GEN_ADAPTER_2_VOID(name, t1, t2) \\
HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2); return 0;
#define GEN_ADAPTER_2_NONVOID(name, t1, t2) \\
return HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2);
#define GEN_ADAPTER_2_DEFINE(name, ret, t1, t2) \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS) { \\
IIF(IS_VOID(ret)) (GEN_ADAPTER_2_VOID(name, t1, t2), GEN_ADAPTER_2_NONVOID(name, t1, t2)) \\
}
#define GEN_ADAPTER_3_VOID(name, t1, t2, t3) \\
HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2, (dh_ctype(t3))A3); return 0;
#define GEN_ADAPTER_3_NONVOID(name, t1, t2, t3) \\
return HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2, (dh_ctype(t3))A3);
#define GEN_ADAPTER_3_DEFINE(name, ret, t1, t2, t3) \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS) { \\
IIF(IS_VOID(ret)) (GEN_ADAPTER_3_VOID(name, t1, t2, t3), GEN_ADAPTER_3_NONVOID(name, t1, t2, t3)) \\
}
#define GEN_ADAPTER_4_VOID(name, t1, t2, t3, t4) \\
HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2, (dh_ctype(t3))A3, (dh_ctype(t4))A4); return 0;
#define GEN_ADAPTER_4_NONVOID(name, t1, t2, t3, t4) \\
return HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2, (dh_ctype(t3))A3, (dh_ctype(t4))A4);
#define GEN_ADAPTER_4_DEFINE(name, ret, t1, t2, t3, t4) \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS) { \\
IIF(IS_VOID(ret)) (GEN_ADAPTER_4_VOID(name, t1, t2, t3, t4), GEN_ADAPTER_4_NONVOID(name, t1, t2, t3, t4)) \\
}
#define GEN_ADAPTER_5_VOID(name, t1, t2, t3, t4, t5) \\
HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2, (dh_ctype(t3))A3, (dh_ctype(t4))A4, (dh_ctype(t5))A5); return 0;
#define GEN_ADAPTER_5_NONVOID(name, t1, t2, t3, t4, t5) \\
return HELPER(name)((dh_ctype(t1))A1, (dh_ctype(t2))A2, (dh_ctype(t3))A3, (dh_ctype(t4))A4, (dh_ctype(t5))A5);
#define GEN_ADAPTER_5_DEFINE(name, ret, t1, t2, t3, t4, t5) \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS) { \\
IIF(IS_VOID(ret)) (GEN_ADAPTER_5_VOID(name, t1, t2, t3, t4, t5), GEN_ADAPTER_5_NONVOID(name, t1, t2, t3, t4, t5)) \\
}
#define GEN_ADAPTER_BLANK
#ifdef GEN_ADAPTER_DEFINE
#define GEN_ADAPTER_0(name, ret) \\
IIF(IS_GLOB(name)) (GEN_ADAPTER_BLANK, GEN_ADAPTER_0_DEFINE(name, ret))
#define GEN_ADAPTER_1(name, ret, t1) \\
IIF(IS_GLOB(name)) (GEN_ADAPTER_BLANK, GEN_ADAPTER_1_DEFINE(name, ret, t1))
#define GEN_ADAPTER_2(name, ret, t1, t2) \\
IIF(IS_GLOB(name)) (GEN_ADAPTER_BLANK, GEN_ADAPTER_2_DEFINE(name, ret, t1, t2))
#define GEN_ADAPTER_3(name, ret, t1, t2, t3) \\
IIF(IS_GLOB(name)) (GEN_ADAPTER_BLANK, GEN_ADAPTER_3_DEFINE(name, ret, t1, t2, t3))
#define GEN_ADAPTER_4(name, ret, t1, t2, t3, t4) \\
IIF(IS_GLOB(name)) (GEN_ADAPTER_BLANK, GEN_ADAPTER_4_DEFINE(name, ret, t1, t2, t3, t4))
#define GEN_ADAPTER_5(name, ret, t1, t2, t3, t4, t5) \\
IIF(IS_GLOB(name)) (GEN_ADAPTER_BLANK, GEN_ADAPTER_5_DEFINE(name, ret, t1, t2, t3, t4, t5))
#else
#define GEN_ADAPTER_0(name, ret) GEN_ADAPTER_BLANK
#define GEN_ADAPTER_1(name, ret, t1) GEN_ADAPTER_BLANK
#define GEN_ADAPTER_2(name, ret, t1, t2) GEN_ADAPTER_BLANK
#define GEN_ADAPTER_3(name, ret, t1, t2, t3) GEN_ADAPTER_BLANK
#define GEN_ADAPTER_4(name, ret, t1, t2, t3, t4) GEN_ADAPTER_BLANK
#define GEN_ADAPTER_5(name, ret, t1, t2, t3, t4, t5) GEN_ADAPTER_BLANK
#endif
"""
def patchUnicornTCI():
"""
Patches Unicorn's QEMU fork to add the TCG Interpreter backend
"""
# Enable TCI
replace(os.path.join(UNICORN_QEMU_DIR, "configure"), {
"strip_opt=\"yes\"": "strip_opt=\"yes\"\ntcg_interpreter=\"yes\"",
"# XXX: suppress that": "if test \"$tcg_interpreter\" = \"yes\" ; then\n echo \"CONFIG_TCG_INTERPRETER=y\" >> $config_host_mak\nfi\n# XXX: suppress that",
"if test \"$ARCH\" = \"sparc64\" ; then": "if test \"$tcg_interpreter\" = \"yes\"; then\n QEMU_INCLUDES=\"-I\$(SRC_PATH)/tcg/tci $QEMU_INCLUDES\"\nelif test \"$ARCH\" = \"sparc64\" ; then"
})
# Add executable permissions for the new configure file
path = os.path.join(UNICORN_QEMU_DIR, "configure")
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)
# Copy missing TCI source files and patch them with Unicorn updates
copytree(ORIGINAL_QEMU_DIR, UNICORN_QEMU_DIR)
replace(os.path.join(UNICORN_QEMU_DIR, "tcg/tci/tcg-target.c"), {
"tcg_target_available_regs": "s->tcg_target_available_regs",
"tcg_target_call_clobber_regs": "s->tcg_target_call_clobber_regs",
"tcg_add_target_add_op_defs(": "tcg_add_target_add_op_defs(s, ",
})
replace(os.path.join(UNICORN_QEMU_DIR, "tcg/tci/tcg-target.h"), {
"#define tcg_qemu_tb_exec": "//#define tcg_qemu_tb_exec",
})
# Add TCI to Makefile.targets
insert(os.path.join(UNICORN_QEMU_DIR, "Makefile.target"),
"obj-y += tcg/tcg.o tcg/optimize.o", [
"obj-$(CONFIG_TCG_INTERPRETER) += tci.o"
]
)
# Add TCI symbols
insert(os.path.join(UNICORN_QEMU_DIR, "header_gen.py"),
"symbols = (", [
" 'tci_tb_ptr',",
" 'tcg_qemu_tb_exec',",
]
)
# Update platform headers with new symbols
cmd = "bash -c \"cd " + UNICORN_QEMU_DIR + " && ./gen_all_header.sh\""
os.system(cmd)
def patchUnicornJS():
"""
Patches Unicorn files to target JavaScript
"""
# Disable unnecessary options
replace(os.path.join(UNICORN_DIR, "config.mk"), {
"UNICORN_DEBUG ?= yes": "UNICORN_DEBUG ?= no",
"UNICORN_SHARED ?= yes": "UNICORN_SHARED ?= no",
})
# Ensure QEMU's object files have different base names
name = "rename_objects.py"
with open(os.path.join(UNICORN_DIR, name), "wt") as f:
f.write("")
replace(os.path.join(UNICORN_DIR, "Makefile"), {
"$(MAKE) -C qemu $(SMP_MFLAGS)":
"$(MAKE) -C qemu $(SMP_MFLAGS)\r\n\t@python " + name,
' ./configure --cc="${CC}" --extra-cflags="$(UNICORN_CFLAGS)" --target-list="$(UNICORN_TARGETS)" ${UNICORN_QEMU_FLAGS}':
' ./configure --cc="${CC}" --extra-cflags="$(UNICORN_CFLAGS)" --target-list="$(UNICORN_TARGETS)" ${UNICORN_QEMU_FLAGS} --disable-stack-protector --cpu=i386',
})
# Replace sigsetjmp/siglongjump with setjmp/longjmp
replace(os.path.join(UNICORN_QEMU_DIR, "cpu-exec.c"), {
"sigsetjmp(cpu->jmp_env, 0)": "setjmp(cpu->jmp_env)",
"siglongjmp(cpu->jmp_env, 1)": "longjmp(cpu->jmp_env, 1)",
})
# Fix Glib function pointer issues
replace(os.path.join(UNICORN_QEMU_DIR, "glib_compat.c"), {
"(GCompareDataFunc) compare_func) (l1->data, l2->data, user_data)":
"(GCompareFunc) compare_func) (l1->data, l2->data)",
})
# Fix QEMU function pointer issues
replace(os.path.join(UNICORN_QEMU_DIR, "include/exec/helper-proto.h"), {
# Adapter helpers
"#include <exec/helper-head.h>":
"#include <exec/helper-head.h>\n" + PATCH_HELPER_ADAPTER_PROTO,
# Declare adapters
"#define DEF_HELPER_FLAGS_0(name, flags, ret) \\":"""
#define DEF_HELPER_FLAGS_0(name, flags, ret) \\
GEN_ADAPTER_DECLARE(name) \\""",
"#define DEF_HELPER_FLAGS_1(name, flags, ret, t1) \\":"""
#define DEF_HELPER_FLAGS_1(name, flags, ret, t1) \\
GEN_ADAPTER_DECLARE(name) \\""",
"#define DEF_HELPER_FLAGS_2(name, flags, ret, t1, t2) \\":"""
#define DEF_HELPER_FLAGS_2(name, flags, ret, t1, t2) \\
GEN_ADAPTER_DECLARE(name) \\""",
"#define DEF_HELPER_FLAGS_3(name, flags, ret, t1, t2, t3) \\":"""
#define DEF_HELPER_FLAGS_3(name, flags, ret, t1, t2, t3) \\
GEN_ADAPTER_DECLARE(name) \\""",
"#define DEF_HELPER_FLAGS_4(name, flags, ret, t1, t2, t3, t4) \\":"""
#define DEF_HELPER_FLAGS_4(name, flags, ret, t1, t2, t3, t4) \\
GEN_ADAPTER_DECLARE(name) \\""",
"#define DEF_HELPER_FLAGS_5(name, flags, ret, t1, t2, t3, t4, t5) \\":"""
#define DEF_HELPER_FLAGS_5(name, flags, ret, t1, t2, t3, t4, t5) \\
GEN_ADAPTER_DECLARE(name) \\""",
})
replace(os.path.join(UNICORN_QEMU_DIR, "include/exec/helper-gen.h"), {
# Adapter helpers
"#include <exec/helper-head.h>":
"#include <exec/helper-head.h>\n" + PATCH_HELPER_ADAPTER_GEN,
# Generate calls to adapters instead
"tcg_gen_callN(tcg_ctx, HELPER(name)":
"tcg_gen_callN(tcg_ctx, glue(adapter_helper_, name)",
# Define adapters
"#define DEF_HELPER_FLAGS_0(name, flags, ret) \\":"""
#define DEF_HELPER_FLAGS_0(name, flags, ret) \\
GEN_ADAPTER_0(name, ret) \\""",
"#define DEF_HELPER_FLAGS_1(name, flags, ret, t1) \\":"""
#define DEF_HELPER_FLAGS_1(name, flags, ret, t1) \\
GEN_ADAPTER_1(name, ret, t1) \\""",
"#define DEF_HELPER_FLAGS_2(name, flags, ret, t1, t2) \\":"""
#define DEF_HELPER_FLAGS_2(name, flags, ret, t1, t2) \\
GEN_ADAPTER_2(name, ret, t1, t2) \\""",
"#define DEF_HELPER_FLAGS_3(name, flags, ret, t1, t2, t3) \\":"""
#define DEF_HELPER_FLAGS_3(name, flags, ret, t1, t2, t3) \\
GEN_ADAPTER_3(name, ret, t1, t2, t3) \\""",
"#define DEF_HELPER_FLAGS_4(name, flags, ret, t1, t2, t3, t4) \\":"""
#define DEF_HELPER_FLAGS_4(name, flags, ret, t1, t2, t3, t4) \\
GEN_ADAPTER_4(name, ret, t1, t2, t3, t4) \\""",
"#define DEF_HELPER_FLAGS_5(name, flags, ret, t1, t2, t3, t4, t5) \\":"""
#define DEF_HELPER_FLAGS_5(name, flags, ret, t1, t2, t3, t4, t5) \\
GEN_ADAPTER_5(name, ret, t1, t2, t3, t4, t5) \\""",
})
replace(os.path.join(UNICORN_QEMU_DIR, "tcg-runtime.c"), {
# Adapter helpers
'#include "exec/helper-head.h"':
'#include "exec/helper-head.h"\n' +
PATCH_HELPER_ADAPTER_GEN,
# Add uc_tracecode to globals
'#include "tcg-runtime.h"':"""
#undef DEF_HELPER_FLAGS_2
#define DEF_HELPER_FLAGS_2(name, flags, ret, t1, t2) \\
dh_ctype(ret) HELPER(name) (dh_ctype(t1), dh_ctype(t2)); \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS); \\
GEN_ADAPTER_2_DEFINE(name, ret, t1, t2)
#define DEF_HELPER_FLAGS_4(name, flags, ret, t1, t2, t3, t4) \\
dh_ctype(ret) HELPER(name) (dh_ctype(t1), dh_ctype(t2), dh_ctype(t3), dh_ctype(t4)); \\
uint64_t glue(adapter_helper_, name)(GEN_ADAPTER_ARGS); \\
GEN_ADAPTER_4_DEFINE(name, ret, t1, t2, t3, t4)
DEF_HELPER_4(uc_tracecode, void, i32, i32, ptr, i64)
#include "tcg-runtime.h"
""",
})
replace(os.path.join(UNICORN_QEMU_DIR, "include/exec/helper-tcg.h"), {
"func = HELPER(NAME)":
"func = glue(adapter_helper_, NAME)"
})
# Add arch-suffixes to adapters
header_gen_patched = False
with open(os.path.join(UNICORN_QEMU_DIR, "header_gen.py"), 'r') as f:
if 'adapter_' in f.read():
header_gen_patched = True
if header_gen_patched == False:
os.remove(os.path.join(UNICORN_QEMU_DIR, "header_gen.py.bak"))
replace(os.path.join(UNICORN_QEMU_DIR, "header_gen.py"), {
' print("#define %s %s_%s" %(s, s, arch))':
' print("#define %s %s_%s" %(s, s, arch))\n'
' if s.startswith("helper_"):\n'
' s = "adapter_" + s\n'
' print("#define %s %s_%s" %(s, s, arch))',
})
# Define adapters
translate_pat = os.path.join(UNICORN_QEMU_DIR, "target-*/translate.c")
for fpath in glob.glob(translate_pat):
prepend(fpath, '#define GEN_ADAPTER_DEFINE\n')
# Fix register allocation for arguments
replace(os.path.join(UNICORN_QEMU_DIR, "tcg/tcg.c"), {
"int is_64bit = ":
"int is_64bit = 1;//",
# Explicit casts of non-64bit arguments in tcg_gen_callN
"sizemask = info->sizemask;":"""
sizemask = info->sizemask;
for (i = 0; i < nargs; i++) {
int is_64bit = sizemask & (1 << (i+1)*2);
if (!is_64bit) {
TCGArg ext_arg = tcg_temp_new_i64(s);
tcg_gen_ext32u_i64(s, ext_arg, args[i]);
args[i] = GET_TCGV_I64(ext_arg);
}
}
"""
})
# Fix unaligned reads
append(os.path.join(UNICORN_QEMU_DIR, "include/qemu-common.h"),
PATCH_UNALIGNED_MEMACCESS)
replace(os.path.join(UNICORN_QEMU_DIR, "include/exec/exec-all.h"), {
" *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);":
" UNALIGNED_WRITE32_LE(jmp_addr, addr - (jmp_addr + 4));"
})
replace(os.path.join(UNICORN_QEMU_DIR, "tci.c"), {
"*(tcg_target_ulong *)(*tb_ptr)":
"UNALIGNED_READ32_LE(*tb_ptr)",
"*(uint32_t *)(*tb_ptr)":
"UNALIGNED_READ32_LE(*tb_ptr)",
"*(int32_t *)(*tb_ptr)":
"UNALIGNED_READ32_LE(*tb_ptr)",
"*(uint64_t *)tb_ptr":
"UNALIGNED_READ64_LE(tb_ptr)",
# Stores
"*(uint16_t *)(t1 + t2) = t0":
"UNALIGNED_WRITE16_LE(t1 + t2, t0)",
"*(uint32_t *)(t1 + t2) = t0":
"UNALIGNED_WRITE32_LE(t1 + t2, t0)",
"*(uint64_t *)(t1 + t2) = t0":
"UNALIGNED_WRITE64_LE(t1 + t2, t0)",
# Loads
"*(uint32_t *)(t1 + t2)":
"UNALIGNED_READ32_LE(t1 + t2)",
"*(uint64_t *)(t1 + t2)":
"UNALIGNED_READ64_LE(t1 + t2)",
"*(int32_t *)(t1 + t2)":
"(int32_t)UNALIGNED_READ32_LE(t1 + t2)",
})
# Fix unsupported varargs in uc_hook_add function signature
replace(os.path.join(UNICORN_DIR, "include/unicorn/unicorn.h"), {
" void *user_data, uint64_t begin, uint64_t end, ...);":
" void *user_data, uint64_t begin, uint64_t end, uint32_t extra);",
})
replace(os.path.join(UNICORN_DIR, "uc.c"), {
" err = uc_hook_add(uc, &uc->count_hook, UC_HOOK_CODE, hook_count_cb, NULL, 1, 0);":
" err = uc_hook_add(uc, &uc->count_hook, UC_HOOK_CODE, hook_count_cb, NULL, 1, 0, 0);",
" void *user_data, uint64_t begin, uint64_t end, ...)":
" void *user_data, uint64_t begin, uint64_t end, uint32_t extra)",
" va_list valist;":
" //va_list valist;",
" va_start(valist, end);":
" //va_start(valist, end);",
" hook->insn = va_arg(valist, int);":
" hook->insn = extra;",
" va_end(valist);":
" //va_end(valist);",
})
############
# Building #
############
def compileUnicorn(targets):
# Patching Unicorn's QEMU fork
patchUnicornTCI()
patchUnicornJS()
# Emscripten: Make
os.chdir('unicorn')
os.system('make clean')
if os.name == 'posix':
cmd = ''
if targets:
cmd += 'UNICORN_ARCHS="%s" ' % (' '.join(targets))
cmd += 'emmake make unicorn'
os.system(cmd)
os.chdir('..')
# Compile static library to JavaScript
methods = ['_malloc', 'ccall', 'getValue', 'setValue', 'addFunction', 'removeFunction', 'writeArrayToMemory']
cmd = 'emcc'
cmd += ' -Os --memory-init-file 0'
cmd += ' unicorn/libunicorn.a'
cmd += ' -s EXPORTED_FUNCTIONS=\"[\''+ '\', \''.join(EXPORTED_FUNCTIONS) +'\']\"'
cmd += ' -s EXPORTED_RUNTIME_METHODS=\"[\''+ '\', \''.join(methods) +'\']\"'
cmd += ' -s RESERVED_FUNCTION_POINTERS=256'
cmd += ' -s ALLOW_MEMORY_GROWTH=1'
cmd += ' -s MODULARIZE=1'
cmd += ' -s WASM_ASYNC_COMPILATION=0'
cmd += ' -s WASM=0'
cmd += ' -s ENVIRONMENT="web"'
cmd += ' -s EXPORT_NAME="\'MUnicorn\'"'
if targets:
cmd += ' -o src/libunicorn-%s.out.js' % ('-'.join(targets))
else:
cmd += ' -o src/libunicorn.out.js'
os.system(cmd)
def exit_usage():
print("Usage: %s <action> [<targets>...]\n" % (sys.argv[0]))
print("List of actions:")
print(" - patch: Patch Unicorn only")
print(" - build: Patch Unicorn and build Unicorn.js")
exit(1)
if __name__ == "__main__":
# Initialize Unicorn submodule if necessary
if not os.listdir(UNICORN_DIR):
os.system("git submodule update --init")
# Compile Unicorn
if len(sys.argv) < 2:
exit_usage()
action = sys.argv[1]
if action == 'patch':
patchUnicornTCI()
patchUnicornJS()
elif action == 'build':
patchUnicornTCI()
patchUnicornJS()
targets = sorted(sys.argv[2:])
if os.name in ['posix']:
generateConstants()
compileUnicorn(targets)
else:
print("Your operating system is not supported by this script:")
print("Please, use Emscripten to compile Unicorn manually to src/libunicorn.out.js")
else:
exit_usage()