-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbuild.py
More file actions
254 lines (222 loc) · 8.33 KB
/
Copy pathbuild.py
File metadata and controls
254 lines (222 loc) · 8.33 KB
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
#!/usr/bin/env python3
import json
import os
import re
import shutil
import subprocess
import sys
import zipfile
EXPORTED_FUNCTIONS = [
'_free',
'_malloc',
'_uc_arch_supported',
'_uc_close',
'_uc_context_alloc',
'_uc_context_free',
'_uc_context_restore',
'_uc_context_save',
'_uc_ctl',
'_uc_emu_start',
'_uc_emu_stop',
'_uc_errno',
'_uc_free',
'_uc_hook_add',
'_uc_hook_del',
'_uc_mem_map_ptr',
'_uc_mem_map',
'_uc_mem_protect',
'_uc_mem_read',
'_uc_mem_regions',
'_uc_mem_unmap',
'_uc_mem_write',
'_uc_open',
'_uc_query',
'_uc_reg_read_batch',
'_uc_reg_read',
'_uc_reg_write_batch',
'_uc_reg_write',
'_uc_strerror',
'_uc_version',
]
AVAILABLE_ARCHITECTURES = [
'arm', 'aarch64', 'm68k', 'mips', 'ppc', 'riscv', 's390x', 'sparc',
'tricore', 'x86',
]
# Unicorn build-target names whose constants file is named differently.
TARGET_ALIASES = {
'aarch64': 'arm64',
}
# Map a build-target arch to the (header, name, UC_ prefix) for its constants
def arch_constants(arch):
name = TARGET_ALIASES.get(arch.lower(), arch.lower())
return name + '.h', name, 'UC_%s_' % name.upper()
# Directories
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
UNICORN_DIR = os.path.join(ROOT_DIR, "unicorn")
UNICORN_INCLUDE_DIR = os.path.join(UNICORN_DIR, "include", "unicorn")
UNICORN_QEMU_DIR = os.path.join(UNICORN_DIR, "qemu")
UNICORN_BUILD_DIR = os.path.join(UNICORN_DIR, "build")
ORIGINAL_QEMU_DIR = os.path.join(ROOT_DIR, "externals/qemu-5.0.1")
HELPER_ADAPTER_SRC = os.path.join(ROOT_DIR, "src/qemu/helper-adapter.h")
def generateConstants():
"""Generate src/constants_<name>.js from Unicorn's C headers (one per header).
Each file is loaded into the module via Emscripten `--post-js` (see
compileUnicorn) so it runs with `Module` in scope and merges its constants
straight onto the module, which becomes the public `uc` object.
Each header's `typedef enum` / `#define` constants are parsed and every value
resolved to an integer, so enum aliases (e.g. `UC_ARM_REG_R13 = UC_ARM_REG_SP`)
and expressions (e.g. `1 << 30`, `UC_X86_REG_EIP + 2`) collapse to plain
integer literals.
"""
for arch in AVAILABLE_ARCHITECTURES:
header, name, prefix = arch_constants(arch)
prefixes = (prefix, 'UC_CPU_')
content = open(os.path.join(UNICORN_INCLUDE_DIR, header)).read()
# Strip C comments up front so values never collide with `//`/`/* */`.
content = re.sub(r'/\*.*?\*/', ' ', content, flags=re.DOTALL)
out = open('src/constants_%s.js' % name, 'w')
out.write('// AUTO-GENERATED, DO NOT EDIT [%s]\n' % header)
out.write('Object.assign(Module, {\n')
values = {} # running namespace for value resolution
count = 0
for line in content.splitlines():
line = line.split('//', 1)[0].strip()
if line == '':
continue
if line.startswith('#define '):
fields = re.split(r'\s+', line[len('#define '):], 1)
if len(fields) != 2 or '(' in fields[0] or ')' in fields[0]:
continue # skip multi-token / function-like macros
line = fields[0] + ' = ' + fields[1]
if not line.startswith(prefixes):
continue
for token in line.split(','):
token = token.strip()
if not token:
continue
fields = re.split(r'\s+', token)
if not fields[0].startswith(prefixes):
continue
if len(fields) > 1 and fields[1] != '=':
continue
if len(fields) > 1 and fields[1] == '=':
rhs = ''.join(fields[2:])
else:
rhs = str(count)
name_c = fields[0].strip()
value = eval(rhs, None, values)
exec('%s = %d' % (name_c, value), None, values)
count = value + 1
key = name_c[3:] if name_c.startswith('UC_') else name_c
out.write(' %s: %d,\n' % (key, value))
out.write('});\n')
out.close()
def constant_files(archs):
"""Per-arch constants files from generateConstants(), loaded via --post-js."""
targets = archs if archs else AVAILABLE_ARCHITECTURES
files = []
for arch in targets:
path = 'src/constants_%s.js' % arch_constants(arch)[1]
if path not in files:
files.append(path)
return files
############
# Patching #
############
# Copy directory contents into another folder without overwriting existing 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)
# Apply patch to repository via git apply (idempotent)
def apply_patch(repo, patch):
patch_path = os.path.join(ROOT_DIR, patch)
already = subprocess.run(
["git", "apply", "--reverse", "--check", patch_path],
cwd=repo, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if already.returncode == 0:
return
subprocess.run(["git", "apply", patch_path], cwd=repo, check=True)
def patchUnicorn():
# Re-add the TCG Interpreter (TCI) to Unicorn's QEMU 5.0.1 fork
copytree(ORIGINAL_QEMU_DIR, UNICORN_QEMU_DIR)
apply_patch(UNICORN_DIR, "src/patches/unicorn-tci.patch")
# Wrap every TCG helper in an adapter a uniform call signature (no function casts in Emscripten)
dst = os.path.join(UNICORN_QEMU_DIR, "include/exec/helper-adapter.h")
if not os.path.exists(dst):
shutil.copy2(HELPER_ADAPTER_SRC, dst)
apply_patch(UNICORN_DIR, "src/patches/unicorn-adapters.patch")
# Add missing PPC symbols
apply_patch(UNICORN_DIR, "src/patches/unicorn-ppc-fix.patch")
subprocess.run(["bash", "symbols.sh"], check=True, cwd=UNICORN_DIR)
############
# Building #
############
def compileUnicorn(archs=[]):
targets = archs if archs else AVAILABLE_ARCHITECTURES
shutil.rmtree(UNICORN_BUILD_DIR, ignore_errors=True)
# Configure with CMake
subprocess.run([
'emcmake', 'cmake',
'-B', UNICORN_BUILD_DIR,
'-S', UNICORN_DIR,
'-DCMAKE_BUILD_TYPE=Release',
'-DBUILD_SHARED_LIBS=OFF',
'-DUNICORN_ARCH=' + ';'.join(targets),
'-DUNICORN_BUILD_TESTS=OFF',
'-DUNICORN_INSTALL=OFF',
'-DUNICORN_FUZZ=OFF',
'-DUNICORN_LEGACY_STATIC_ARCHIVE=ON',
], check=True)
# Build the static library
jobs = os.cpu_count() or 1
cmd = ['emmake', 'cmake', '--build', UNICORN_BUILD_DIR, '--target', 'unicorn_archive', f'-j{jobs}']
subprocess.run(cmd, check=True)
# Port the static library to JavaScript/WASM
suffix = ('_' + '+'.join(archs)) if archs else ''
methods = [
'ccall', 'getValue', 'setValue', 'addFunction', 'removeFunction', 'writeArrayToMemory'
]
cmd = [
'emcc',
'-Os',
os.path.join(UNICORN_BUILD_DIR, 'libunicorn.a'),
'-s', f"EXPORTED_FUNCTIONS={EXPORTED_FUNCTIONS}",
'-s', f"EXPORTED_RUNTIME_METHODS={methods}",
'-s', 'RESERVED_FUNCTION_POINTERS=256',
'-s', 'ALLOW_TABLE_GROWTH=1',
'-s', 'ALLOW_MEMORY_GROWTH=1',
'-s', 'MODULARIZE=1',
'-s', 'SINGLE_FILE=1',
'-s', 'WASM=1',
'-s', 'WASM_BIGINT=1',
'-s', "EXPORT_NAME='MUnicorn'",
]
for path in constant_files(archs):
cmd += ['--post-js', path]
cmd += ['--post-js', 'src/unicorn-wrapper.js']
cmd += ['-o', f'dist/unicorn{suffix}.js']
os.makedirs('dist', exist_ok=True)
subprocess.run(cmd, check=True)
if __name__ == "__main__":
# Initialize Unicorn submodule if necessary
if not os.listdir(UNICORN_DIR):
os.system("git submodule update --init")
# Patch Unicorn submodule
patchUnicorn()
args = sys.argv[1:]
release = '--release' in args
generateConstants()
if release:
compileUnicorn([]) # Build all
for arch in AVAILABLE_ARCHITECTURES:
compileUnicorn([arch])
else:
archs = sorted(a for a in args if not a.startswith('--'))
compileUnicorn(archs)