diff --git a/emcc.py b/emcc.py index 824c040cac9ee..3b3ea0d75f844 100755 --- a/emcc.py +++ b/emcc.py @@ -2605,6 +2605,8 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target, if not shared.Settings.WASM_BACKEND and not DEBUG: os.unlink(asm_target) # we don't need the asm.js, it can just confuse sys.exit(0) # and we are done. + else: + shared.WebAssembly.add_emscripten_metadata(final, wasm_binary_target) if options.opt_level >= 2: # minify the JS optimizer.do_minify() # calculate how to minify diff --git a/tools/shared.py b/tools/shared.py index 4df0108a4e982..1f09e178cf71f 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -507,6 +507,14 @@ def get_emscripten_version(path): EMSCRIPTEN_VERSION = get_emscripten_version(path_from_root('emscripten-version.txt')) parts = [int(x) for x in EMSCRIPTEN_VERSION.split('.')] EMSCRIPTEN_VERSION_MAJOR, EMSCRIPTEN_VERSION_MINOR, EMSCRIPTEN_VERSION_TINY = parts +# For the Emscripten-specific WASM metadata section, follows semver, changes +# whenever metadata section changes structure +# NB: major version 0 implies no compatibility +(EMSCRIPTEN_METADATA_MAJOR, EMSCRIPTEN_METADATA_MINOR) = (0, 0) +# For the JS/WASM ABI, follows semver, changes whenever musl C types +# change size/signedness or syscalls change signature +# NB: major version 0 implies no backward- compatibility +(EMSCRIPTEN_ABI_MAJOR, EMSCRIPTEN_ABI_MINOR) = (0, 0) def generate_sanity(): @@ -2974,16 +2982,61 @@ def lebify(x): return bytearray(ret) @staticmethod - def make_shared_library(js_file, wasm_file, needed_dynlibs): - # a wasm shared library has a special "dylink" section, see tools-conventions repo + def get_js_data(js_file, shared=False): js = open(js_file).read() m = re.search("var STATIC_BUMP = (\d+);", js) mem_size = int(m.group(1)) m = re.search("Module\['wasmTableSize'\] = (\d+);", js) table_size = int(m.group(1)) - m = re.search('gb = alignMemory\(getMemory\(\d+ \+ (\d+)\), (\d+) \|\| 1\);', js) - assert m.group(1) == m.group(2), 'js must contain a clear alignment for the wasm shared library' - mem_align = int(m.group(1)) + if shared: + m = re.search('gb = alignMemory\(getMemory\(\d+ \+ (\d+)\), (\d+) \|\| 1\);', js) + assert m.group(1) == m.group(2), 'js must contain a clear alignment for the wasm shared library' + mem_align = int(m.group(1)) + else: + mem_align = None + return (mem_size, table_size, mem_align) + + @staticmethod + def add_emscripten_metadata(js_file, wasm_file): + (mem_size, table_size, _) = WebAssembly.get_js_data(js_file) + wasm = open(wasm_file, 'rb').read() + + raw_name = b'emscripten_metadata' + name = bytes(bytearray([len(raw_name)])) + raw_name + contents = b''.join(map(bytes, map(WebAssembly.lebify, [ + # metadata section version + EMSCRIPTEN_METADATA_MAJOR, + EMSCRIPTEN_METADATA_MINOR, + # NB: The structure of the following should only be changed + # if EMSCRIPTEN_METADATA_MAJOR is incremented + # ABI version + EMSCRIPTEN_ABI_MAJOR, + EMSCRIPTEN_ABI_MINOR, + # static bump + mem_size, + # table size + table_size, + # NB: more data can be appended here as long as you increase + # the EMSCRIPTEN_METADATA_MINOR + ]))) + + new_section = b''.join([ + b'\0', # custom section byte ID (== 0) + bytes(WebAssembly.lebify(len(name) + len(contents))), + name, + contents, + ]) + + with open(wasm_file, 'wb') as f: + f.write(wasm[0:8]) # copy magic number and version + # Emscripten metadata should be before any standard wasm section + f.write(new_section) + f.write(wasm[8:]) + + @staticmethod + def make_shared_library(js_file, wasm_file, needed_dynlibs): + # a wasm shared library has a special "dylink" section, see tools-conventions repo + (mem_size, table_size, mem_align) = WebAssembly.get_js_data(js_file, True) mem_align = int(math.log(mem_align, 2)) logger.debug('creating wasm dynamic library with mem size %d, table size %d, align %d' % (mem_size, table_size, mem_align)) wso = js_file + '.wso'