Skip to content

Commit 2532c4e

Browse files
committed
Implement --emit-symbol-map locally without calling out wasm-opt
The binaryen version was outputting strange mangled C++ names. Doing this in python should be faster since we don't need to parse the whole binary. Still needs a test. Fixes: #24982
1 parent db05897 commit 2532c4e

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

test/other/test_symbol_map.O2.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
0:run_js
22
1:emscripten_asm_const_int
33
2:__wasm_call_ctors
4-
3:cpp_func\28int\29
4+
3:cpp_func(int)
55
4:middle
66
5:main
77
6:_emscripten_stack_restore

test/other/test_symbol_map.O3.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
1:emscripten_asm_const_int
33
2:middle
44
3:main
5-
4:cpp_func\28int\29
5+
4:cpp_func(int)
66
5:__wasm_call_ctors

tools/building.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,24 +1081,40 @@ def instrument_js_for_safe_heap(js_file):
10811081
return acorn_optimizer(js_file, ['safeHeap'])
10821082

10831083

1084+
def read_name_section(wasm_file):
1085+
with webassembly.Module(wasm_file) as module:
1086+
for section in module.sections():
1087+
if section.type == webassembly.SecType.CUSTOM:
1088+
module.seek(section.offset)
1089+
if module.read_string() == 'name':
1090+
name_map = {}
1091+
# The name section is made up sub-section.
1092+
# We are looking for the function names sub-section
1093+
while module.tell() < section.offset + section.size:
1094+
name_type = module.read_uleb()
1095+
subsection_size = module.read_uleb()
1096+
subsection_end = module.tell() + subsection_size
1097+
if name_type == webassembly.NameType.FUNCTION:
1098+
# We found the function names sub-section
1099+
num_names = module.read_uleb()
1100+
for _ in range(num_names):
1101+
id = module.read_uleb()
1102+
name = module.read_string()
1103+
name_map[id] = name
1104+
return name_map
1105+
module.seek(subsection_end)
1106+
1107+
return name_map
1108+
1109+
10841110
@ToolchainProfiler.profile()
1085-
def handle_final_wasm_symbols(wasm_file, symbols_file, debug_info):
1111+
def write_symbol_map(wasm_file, symbols_file):
10861112
logger.debug('handle_final_wasm_symbols')
1087-
args = []
1088-
if symbols_file:
1089-
args += ['--print-function-map']
1090-
else:
1091-
# suppress the wasm-opt warning regarding "no output file specified"
1092-
args += ['--quiet']
1093-
output = run_wasm_opt(wasm_file, args=args, stdout=PIPE)
1094-
if symbols_file:
1095-
utils.write_file(symbols_file, output)
1096-
if not debug_info:
1097-
# strip the names section using llvm-objcopy. this is slightly slower than
1098-
# using wasm-opt (we could run wasm-opt without -g here and just tell it to
1099-
# write the file back out), but running wasm-opt would undo StackIR
1100-
# optimizations, if we did those.
1101-
strip(wasm_file, wasm_file, sections=['name'])
1113+
names = read_name_section(wasm_file)
1114+
assert(names)
1115+
strings = [f'{id}:{name}' for id, name in names.items()]
1116+
contents = '\n'.join(strings) + '\n'
1117+
utils.write_file(symbols_file, contents)
11021118

11031119

11041120
def is_ar(filename):

tools/link.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,8 +2413,9 @@ def phase_binaryen(target, options, wasm_target):
24132413
if options.emit_symbol_map:
24142414
intermediate_debug_info -= 1
24152415
if generating_wasm:
2416-
building.handle_final_wasm_symbols(wasm_file=wasm_target, symbols_file=symbols_file, debug_info=intermediate_debug_info)
2417-
save_intermediate_with_wasm('symbolmap', wasm_target)
2416+
building.write_symbol_map(wasm_target, symbols_file)
2417+
if not intermediate_debug_info:
2418+
building.strip(wasm_target, wasm_target, sections=['name'])
24182419

24192420
if settings.GENERATE_DWARF and settings.SEPARATE_DWARF and generating_wasm:
24202421
# if the dwarf filename wasn't provided, use the default target + a suffix

tools/webassembly.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ class TargetFeaturePrefix(IntEnum):
157157
DISALLOWED = 0x2d
158158

159159

160+
class NameType(IntEnum):
161+
MODULE = 0
162+
FUNCTION = 1
163+
LOCAL = 2
164+
LABEL = 3
165+
TYPE = 4
166+
TABLE = 5
167+
MEMORY = 6
168+
GLOBAL = 7
169+
ELEMSEGMENT = 8
170+
DATASEGMENT = 9
171+
FIELD = 10
172+
TAG = 11
173+
174+
160175
class InvalidWasmError(BaseException):
161176
pass
162177

0 commit comments

Comments
 (0)