-
Notifications
You must be signed in to change notification settings - Fork 4
/
wasm2dbg.py
executable file
·62 lines (51 loc) · 1.64 KB
/
wasm2dbg.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
#!/usr/bin/env python3
"""
Project: Wasm3
File: wasm2dbg.py
Author: Volodymyr Shymanskyy
Created: 18.06.2020
"""
import re
import tempfile
import os
from shutil import which
infile = "test_app_c/app.wasm"
outfile = infile + ".dbg"
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
sections = [
"CODE",
".debug_info", ".debug_macinfo", ".debug_ranges",
".debug_abbrev", ".debug_line", ".debug_str"
]
utils = dotdict({
"objdump": "llvm-objdump-10",
"objcopy": "objcopy",
"tail": "tail",
"xxd": "xxd",
})
for name, exe in utils.items():
if not which(exe):
raise Exception(f"{exe} utility not found")
tmp = tempfile.TemporaryDirectory()
# Extract DWARF info from WASM file
for sect in sections:
os.system(f"{utils.objdump} -s --section {sect} {infile} | {utils.tail} -n +5 | {utils.xxd} -r > {tmp.name}/section-{sect}")
# Create a dummy binary file
with open(f"{tmp.name}/empty.bin", 'w') as f:
f.write("wasm")
# Create an ELF container with DWARF info
os.system(f"""
{utils.objcopy} -I binary -O elf32-i386 -B i386 \
--add-section .debug_str={tmp.name}/section-.debug_str \
--add-section .debug_line={tmp.name}/section-.debug_line \
--add-section .debug_abbrev={tmp.name}/section-.debug_abbrev \
--add-section .debug_ranges={tmp.name}/section-.debug_ranges \
--add-section .debug_macinfo={tmp.name}/section-.debug_macinfo \
--add-section .debug_info={tmp.name}/section-.debug_info \
--add-section .text={tmp.name}/section-CODE \
{tmp.name}/empty.bin {outfile}
""")