-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathsimpasm
More file actions
executable file
·488 lines (417 loc) · 16 KB
/
simpasm
File metadata and controls
executable file
·488 lines (417 loc) · 16 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
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
#!/usr/bin/env python3
# Copyright (c) The mlkem-native project authors
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
import subprocess
import argparse
import logging
import pathlib
import tempfile
import platform
import sys
import os
import re
def patchup_disasm(asm, cfify=False):
asm = asm.split("\n")
indentation = 8
def decode_label(asm_line):
r = re.search(r"^\s*[0-9a-fA-F]+\s*<([a-zA-Z0-9_]+)>:\s*$", asm_line)
if r is None:
return None
return r.group(1)
def make_label(lbl):
if cfify:
return "L" + lbl + ":"
return lbl + ":"
# Find first label
for i, line in enumerate(asm):
if decode_label(line) is not None:
break
asm = asm[i + 1 :]
def gen(asm):
for line in asm:
if line.strip() == "":
yield ""
continue
lbl = decode_label(line)
# Re-format labels as assembly labels
if lbl is not None:
yield make_label(lbl)
continue
# Drop comments
line = line.split(";")[0]
# Re-format references to labels
# Those are assumed to have the form `0xDEADBEEF <label>`
if cfify:
line = re.sub(
r"(0x)?[0-9a-fA-F]+\s+<(?P<label>[a-zA-Z0-9_]+)>",
r"L\g<label>",
line,
)
else:
line = re.sub(
r"(0x)?[0-9a-fA-F]+\s+<(?P<label>[a-zA-Z0-9_]+)>",
r"\g<label>",
line,
)
# Drop address and byte code from line
d = re.search(
r"^\s*[0-9a-fA-F]+:\s+([0-9a-fA-F][0-9a-fA-F][ ]*)+\s+(?P<inst>.*)$",
line,
)
if d is None:
raise Exception(
f'The following does not seem to be an assembly line of the expected format `ADDRESS: BYTECODE INSTRUCTION`:\n"{line}"'
)
yield " " * indentation + d.group("inst").expandtabs(1)
return list(gen(asm))
def find_header_footer(asm, filename):
header_end_marker = "simpasm: header-end"
footer_start_marker = "simpasm: footer-start"
# Extract header
header_end = None
for i, line in enumerate(asm):
if header_end_marker in line:
header_end = i
break
if header_end is None:
raise Exception(
f"Could not find header-end marker {header_end_marker} in {filename}"
)
header = asm[:header_end]
# Extract footer
footer_start = None
for i, line in enumerate(asm):
if footer_start_marker in line:
footer_start = i
break
if footer_start is None:
raise Exception(
f"Could not find footer-start marker {footer_start_marker} in {filename}"
)
footer = asm[footer_start + 1 :]
body = asm[header_end + 1 : footer_start]
return header, body, footer
def find_globals(asm):
global_symbols = []
for line in asm:
r = re.search(r"^\s*\.global\s+(.*)$", line)
if r is None:
continue
global_symbols.append(r.group(1))
return global_symbols
# Converts `#if ...` statements into `#if 1` in header to avoid having
# to specify various `-D...` in the CFLAGS. The original header will be
# reinstated in the final assembly, so the output is subject to the same
# guards as the input.
def drop_if_from_header(header):
header_new = []
i = 0
while i < len(header):
line = header[i]
if not line.strip().startswith("#if"):
header_new.append(line)
i += 1
continue
header_new.append("#if 1")
while i < len(header) and header[i].endswith("\\"):
i += 1
i += 1
return header_new
def drop_preprocessor_directives(header):
header_new = []
i = 0
while i < len(header):
line = header[i]
if not line.strip().startswith("#"):
header_new.append(line)
i += 1
continue
while i < len(header) and header[i].endswith("\\"):
i += 1
i += 1
return header_new
def simplify(logger, args, asm_input, asm_output=None):
def run_cmd(cmd, input=None):
logger.debug(f"Running command: {' '.join(cmd)}")
try:
r = subprocess.run(
cmd, capture_output=True, input=input, text=True, check=True
)
return r
except subprocess.CalledProcessError as e:
logger.error(f"Command failed: {' '.join(cmd)}")
logger.error(f"Exit code: {e.returncode}")
logger.error(f"stderr: {e.stderr}")
raise Exception("simpasm failed") from e
if asm_output is None:
asm_output = asm_input
# Load input assembly
with open(asm_input, "r") as f:
orig_asm = f.read().split("\n")
header, body, footer = find_header_footer(orig_asm, asm_input)
# Extract unique global symbol from assembly
syms = find_globals(orig_asm)
if len(syms) != 1:
logger.error(
f"Expected exactly one global symbol in {asm_input}, but found {syms}"
)
raise Exception("simpasm failed")
sym = syms[0]
if args.cflags is not None:
cflags = args.cflags.split(" ")
else:
cflags = []
# Create temporary object files for byte code before/after simplification
with (
tempfile.NamedTemporaryFile(suffix=".o", delete=False) as tmp0,
tempfile.NamedTemporaryFile(suffix=".o", delete=False) as tmp1,
):
tmp_objfile0 = tmp0.name
tmp_objfile1 = tmp1.name
cmd = (
[args.cc, "-c", "-x", "assembler-with-cpp"]
+ cflags
+ ["-o", tmp_objfile0, "-"]
)
logger.debug(f"Assembling {asm_input} ...")
asm_no_if = "\n".join(drop_if_from_header(header) + body + footer)
run_cmd(cmd, input=asm_no_if)
# Remember the binary contents of the object file for later
tmp0.seek(0)
orig_obj = tmp0.read()
# Check that there is exactly one global symbol at location 0
cmd = [args.nm, "--extern-only", tmp_objfile0]
logger.debug(
f"Extracting symbols from temporary object file {tmp_objfile0} ..."
)
r = run_cmd(cmd)
nm_output = r.stdout.split("\n")
nm_output = list(filter(lambda s: s.strip() != "", nm_output))
if len(nm_output) == 0:
logger.error(
f"Found one .global annotation in {asm_input}, but no external symbols in object file {tmp_objfile0} -- should not happen?"
)
logger.error(asm_no_if)
raise Exception("simpasm failed")
elif len(nm_output) > 1:
logger.error(
f"Found only one .global annotation in {asm_input}, but multiple external symbols {nm_output} in object file -- should not happen?"
)
raise Exception("simpasm failed")
sym_info = nm_output[0].split(" ")
sym_addr = int(sym_info[0], 16)
if sym_addr != 0:
logger.error(
f"Global sym {sym} not at address 0 (instead at address {hex(sym_addr)}) -- please reorder the assembly to start with the global function symbol"
)
raise Exception("simpasm failed")
# If we don't preserve preprocessor directives, use the raw global symbol name instead;
# otherwise, end up emitting a namespaced symbol without including the header needed to
# make sense of it.
if args.preserve_preprocessor_directives is False:
# Expecting format "ADDRESS T SYMBOL"
sym = sym_info[2]
logger.debug(f"Using raw global symbol {sym} going forward ...")
cmd = [args.objdump, "--disassemble", tmp_objfile0]
if platform.system() == "Darwin" and args.arch == "aarch64":
cmd += ["--triple=aarch64"]
# Armv8.1-M requires explicit triple for Thumb disassembly
if args.arch == "armv81m":
cmd += ["--triple=thumbv8.1m.main-none-eabi"]
# Add syntax option if specified
if args.syntax and args.syntax.lower() != "att":
cmd += [f"--x86-asm-syntax={args.syntax}"]
logger.debug(f"Disassembling temporary object file {tmp_objfile0} ...")
disasm = run_cmd(cmd).stdout
logger.debug("Patching up disassembly ...")
simplified = patchup_disasm(disasm, cfify=args.cfify)
# Mark stack as non-executable for ELF targets.
# This must be outside any preprocessor guards to ensure the
# .note.GNU-stack section is always emitted.
elf_stack_section = [
"#if defined(__ELF__)",
'.section .note.GNU-stack,"",%progbits',
"#endif",
"",
]
autogen_header = [
"",
"/*",
" * WARNING: This file is auto-derived from the mlkem-native source file",
f" * {asm_input} using scripts/simpasm. Do not modify it directly.",
" */",
"",
]
# Add Thumb directives for Armv8.1-M
if args.arch == "armv81m":
autogen_header += [".thumb", ".syntax unified", ""]
autogen_header += [".text", ".balign 4"]
# Add syntax specifier for Intel syntax
if args.syntax and args.syntax.lower() == "intel":
autogen_header.append(".intel_syntax noprefix")
if args.preserve_preprocessor_directives is False:
if platform.system() == "Darwin" and sym[0] == "_":
sym = sym[1:]
autogen_header += [
"#ifdef __APPLE__",
f".global _{sym}",
f"_{sym}:",
"#else",
f".global {sym}",
f"{sym}:",
"#endif",
"",
]
simplified_header = drop_preprocessor_directives(header)
simplified_footer = []
else:
autogen_header += [
f".global {sym}",
f"{sym.replace('MLK_ASM_NAMESPACE', 'MLK_ASM_FN_SYMBOL')}",
"",
]
simplified_header = header
simplified_footer = [
f"{sym.replace('MLK_ASM_NAMESPACE', 'MLK_ASM_FN_SIZE')}",
"",
] + footer
# Apply CFI directives if requested
if args.cfify:
logger.debug("Applying CFI directives...")
cfify_script = os.path.join(os.path.dirname(__file__), "cfify")
cfify_cmd = [cfify_script, "--emit-cfi-proc-start", "--arch", args.arch]
cfify_result = run_cmd(cfify_cmd, input="\n".join(simplified))
simplified = cfify_result.stdout.split("\n")
# Write simplified assembly file
full_simplified = (
simplified_header
+ autogen_header
+ simplified
+ simplified_footer
+ elf_stack_section
)
logger.debug(f"Writing simplified assembly to {asm_output} ...")
with open(asm_output, "w+") as f:
f.write("\n".join(full_simplified))
cmd = (
[args.cc, "-c", "-x", "assembler-with-cpp"]
+ cflags
+ ["-o", tmp_objfile1, "-"]
)
new_asm = "\n".join(
drop_if_from_header(simplified_header)
+ autogen_header
+ simplified
+ simplified_footer
+ elf_stack_section
)
logger.debug("Assembling simplified assembly ...")
logger.debug(new_asm)
logger.debug(f"Command: {' '.join(cmd)}")
run_cmd(cmd, input=new_asm)
# Get binary contents of re-assembled object file
tmp1.seek(0)
simplified_obj = tmp1.read()
logger.debug("Checking that byte-code is unchanged ...")
# When CFI is enabled or for Armv8.1-M, compare only the __text section content
if args.cfify or args.arch == "armv81m":
logger.debug("Comparing __text section content for CFI comparison...")
# Extract __text section from both files
orig_text_cmd = [
args.objdump,
"-s",
"--section=__text",
"--section=.text",
tmp_objfile0,
]
simplified_text_cmd = [
args.objdump,
"-s",
"--section=__text",
"--section=.text",
tmp_objfile1,
]
orig_text_result = run_cmd(orig_text_cmd)
simplified_text_result = run_cmd(simplified_text_cmd)
# Remove filename lines from both outputs
orig_lines = [
line
for line in orig_text_result.stdout.split("\n")
if tmp_objfile0 not in line
]
simplified_lines = [
line
for line in simplified_text_result.stdout.split("\n")
if tmp_objfile1 not in line
]
if orig_lines != simplified_lines:
logger.error(
f"__text section content of {tmp_objfile0} and {tmp_objfile1} before/after simplification differs -- aborting"
)
logger.error("I'll keep them around for you to have a look")
raise Exception("simpasm failed")
else:
if orig_obj != simplified_obj:
logger.error(
f"Object files {tmp_objfile0} and {tmp_objfile1} before/after simplification are not byte-wise equal -- aborting"
)
logger.error("I'll keep them around for you to have a look")
raise Exception("simpasm failed")
os.unlink(tmp_objfile0)
os.unlink(tmp_objfile1)
logger.info(f"Simplified {asm_input} -> {asm_output} (same byte code)")
def _main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("-d", "--directory", type=str, help="Input assembly file")
parser.add_argument("-i", "--input", type=str, help="Input assembly file")
parser.add_argument("-o", "--output", type=str, help="Output assembly file")
parser.add_argument(
"-p", "--preserve-preprocessor-directives", default=False, action="store_true"
)
parser.add_argument("-v", "--verbose", default=False, action="store_true")
parser.add_argument(
"--cc", type=str, default="gcc" if platform.system() != "Darwin" else "clang"
)
parser.add_argument("--nm", type=str, default="nm")
parser.add_argument("--objdump", type=str, default="objdump")
parser.add_argument("--strip", type=str, default="llvm-strip")
parser.add_argument("--cflags", type=str)
parser.add_argument("--cfify", action="store_true", help="Apply CFI directives")
parser.add_argument(
"--arch",
type=str,
default="aarch64",
help="Target architecture for CFI directives",
)
parser.add_argument(
"--syntax",
type=str,
choices=["att", "intel"],
default="att",
help="Assembly syntax for disassembly output (att or intel)",
)
args = parser.parse_args()
os.chdir(os.path.join(os.path.dirname(__file__), ".."))
if (
args.cflags is not None
and args.cflags.startswith('"')
and args.cflags.endswith('"')
):
args.cflags = args.cflags[1:-1]
logging.basicConfig(stream=sys.stdout, format="%(name)s: %(message)s")
logger = logging.getLogger("simpasm")
if args.verbose is True:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
if args.input is not None:
simplify(logger, args, args.input, args.output)
if args.directory is not None:
# Simplify all files in directory
asm_files = pathlib.Path(args.directory).glob("*.S")
for f in asm_files:
simplify(logger, args, f)
if __name__ == "__main__":
_main()