Skip to content

Commit 99cad87

Browse files
authored
wasm-emscripten-finalize: ensure table/memory imports use emscripten's expected names (#1795)
This means lld can emscripten can disagree about the naming of these imports and emscripten-wasm-finalize will take care of paper over the differences.
1 parent d53c648 commit 99cad87

10 files changed

+39
-28
lines changed

auto_update_tests.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ def update_asm_js_tests():
7474
def update_lld_tests():
7575
print '\n[ checking wasm-emscripten-finalize testcases... ]\n'
7676

77-
extension_arg_map = {
78-
'.out': [],
79-
'.jscall.out': ['--emscripten-reserved-function-pointers=3'],
80-
}
8177
for wast_path in files_with_pattern('test', 'lld', '*.wast'):
8278
print '..', wast_path
79+
mem_file = wast_path + '.mem'
80+
extension_arg_map = {
81+
'.out': [],
82+
'.jscall.out': ['--emscripten-reserved-function-pointers=3'],
83+
'.mem.out': ['--separate-data-segments', mem_file],
84+
}
8385
for ext, ext_args in extension_arg_map.items():
8486
out_path = wast_path + ext
8587
if ext != '.out' and not os.path.exists(out_path):

src/tools/wasm-emscripten-finalize.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,28 @@ int main(int argc, const char *argv[]) {
168168

169169
std::vector<Name> initializerFunctions;
170170

171+
// The names of standard imports/exports used by lld doesn't quite match that
172+
// expected by emscripten.
173+
// TODO(sbc): Unify these
174+
if (Export* ex = wasm.getExportOrNull("__wasm_call_ctors")) {
175+
ex->name = "__post_instantiate";
176+
}
177+
if (wasm.table.imported()) {
178+
if (wasm.table.base != "table") wasm.table.base = Name("table");
179+
}
180+
if (wasm.memory.imported()) {
181+
if (wasm.table.base != "memory") wasm.memory.base = Name("memory");
182+
}
183+
171184
if (isSideModule) {
172185
generator.replaceStackPointerGlobal();
173-
// rename __wasm_call_ctors to __post_instantiate which is what
174-
// emscripten expects.
175-
// TODO(sbc): Unify these two names
176-
if (Export* ex = wasm.getExportOrNull("__wasm_call_ctors")) {
177-
ex->name = "__post_instantiate";
178-
}
179186
} else {
180187
generator.generateRuntimeFunctions();
181188
generator.generateMemoryGrowthFunction();
182-
if (wasm.getFunctionOrNull("__wasm_call_ctors")) {
183-
initializerFunctions.push_back("__wasm_call_ctors");
189+
// emscripten calls this by default for side libraries so we only need
190+
// to include in as a static ctor for main module case.
191+
if (wasm.getFunctionOrNull("__post_instantiate")) {
192+
initializerFunctions.push_back("__post_instantiate");
184193
}
185194
}
186195

test/lld/duplicate_imports.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
(global $global$1 i32 (i32.const 66128))
2121
(global $global$2 i32 (i32.const 581))
2222
(export "memory" (memory $0))
23-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
23+
(export "__post_instantiate" (func $__wasm_call_ctors))
2424
(export "main" (func $main))
2525
(export "__heap_base" (global $global$1))
2626
(export "__data_end" (global $global$2))
@@ -101,4 +101,4 @@
101101
)
102102
)
103103
)
104-
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": ["__wasm_call_ctors"], "declares": ["puts"], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": ["invoke_ffd"] }
104+
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": [], "declares": ["puts"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": ["invoke_ffd"] }

test/lld/em_asm.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
(global $global$1 i32 (i32.const 66192))
1818
(global $global$2 i32 (i32.const 652))
1919
(export "memory" (memory $0))
20-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
20+
(export "__post_instantiate" (func $__wasm_call_ctors))
2121
(export "main" (func $main))
2222
(export "__heap_base" (global $global$1))
2323
(export "__data_end" (global $global$2))
@@ -75,4 +75,4 @@
7575
)
7676
)
7777
)
78-
;; METADATA: { "asmConsts": {"2": ["{ Module.print(\"Got \" + $0); }", ["ii"], [""]],"0": ["{ Module.print(\"Hello world\"); }", ["i"], [""]],"1": ["{ return $0 + $1; }", ["iii"], [""]]},"staticBump": 84, "initializers": ["__wasm_call_ctors"], "declares": [], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }
78+
;; METADATA: { "asmConsts": {"2": ["{ Module.print(\"Got \" + $0); }", ["ii"], [""]],"0": ["{ Module.print(\"Hello world\"); }", ["i"], [""]],"1": ["{ return $0 + $1; }", ["iii"], [""]]},"staticBump": 84, "initializers": [], "declares": [], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }

test/lld/hello_world.wast.mem.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(global $global$1 i32 (i32.const 66128))
1111
(global $global$2 i32 (i32.const 581))
1212
(export "memory" (memory $0))
13-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
13+
(export "__post_instantiate" (func $__wasm_call_ctors))
1414
(export "main" (func $main))
1515
(export "__heap_base" (global $global$1))
1616
(export "__data_end" (global $global$2))
@@ -58,4 +58,4 @@
5858
)
5959
)
6060
)
61-
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": ["__wasm_call_ctors"], "declares": ["puts"], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }
61+
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": [], "declares": ["puts"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }

test/lld/hello_world.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
(global $global$1 i32 (i32.const 66128))
1212
(global $global$2 i32 (i32.const 581))
1313
(export "memory" (memory $0))
14-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
14+
(export "__post_instantiate" (func $__wasm_call_ctors))
1515
(export "main" (func $main))
1616
(export "__heap_base" (global $global$1))
1717
(export "__data_end" (global $global$2))
@@ -59,4 +59,4 @@
5959
)
6060
)
6161
)
62-
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": ["__wasm_call_ctors"], "declares": ["puts"], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }
62+
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": [], "declares": ["puts"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }

test/lld/init.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(global $global$1 i32 (i32.const 66112))
99
(global $global$2 i32 (i32.const 576))
1010
(export "memory" (memory $0))
11-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
11+
(export "__post_instantiate" (func $__wasm_call_ctors))
1212
(export "main" (func $main))
1313
(export "__heap_base" (global $global$1))
1414
(export "__data_end" (global $global$2))
@@ -71,4 +71,4 @@
7171
)
7272
)
7373
)
74-
;; METADATA: { "asmConsts": {},"staticBump": 8, "initializers": ["__wasm_call_ctors"], "declares": [], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }
74+
;; METADATA: { "asmConsts": {},"staticBump": 8, "initializers": [], "declares": [], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }

test/lld/recursive.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
(global $global$1 i32 (i32.const 66128))
1212
(global $global$2 i32 (i32.const 587))
1313
(export "memory" (memory $0))
14-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
14+
(export "__post_instantiate" (func $__wasm_call_ctors))
1515
(export "main" (func $main))
1616
(export "__heap_base" (global $global$1))
1717
(export "__data_end" (global $global$2))
@@ -117,4 +117,4 @@
117117
)
118118
)
119119
)
120-
;; METADATA: { "asmConsts": {},"staticBump": 19, "initializers": ["__wasm_call_ctors"], "declares": ["printf"], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }
120+
;; METADATA: { "asmConsts": {},"staticBump": 19, "initializers": [], "declares": ["printf"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }

test/lld/reserved_func_ptr.wast.jscall.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
(global $global$1 i32 (i32.const 66112))
3333
(global $global$2 i32 (i32.const 568))
3434
(export "memory" (memory $0))
35-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
35+
(export "__post_instantiate" (func $__wasm_call_ctors))
3636
(export "main" (func $main))
3737
(export "__heap_base" (global $global$1))
3838
(export "__data_end" (global $global$2))
@@ -294,4 +294,4 @@
294294
)
295295
)
296296
)
297-
;; METADATA: { "asmConsts": {},"staticBump": 0, "initializers": ["__wasm_call_ctors"], "jsCallStartIndex": 3, "jsCallFuncType": ["ddi","fffi","iii","v","vi","viii"], "declares": ["_Z4atoiPKc"], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory","_dynCall_viii"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory","dynCall_viii"], "invokeFuncs": [] }
297+
;; METADATA: { "asmConsts": {},"staticBump": 0, "initializers": [], "jsCallStartIndex": 3, "jsCallFuncType": ["ddi","fffi","iii","v","vi","viii"], "declares": ["_Z4atoiPKc"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory","_dynCall_viii"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory","dynCall_viii"], "invokeFuncs": [] }

test/lld/reserved_func_ptr.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
(global $global$1 i32 (i32.const 66112))
1717
(global $global$2 i32 (i32.const 568))
1818
(export "memory" (memory $0))
19-
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
19+
(export "__post_instantiate" (func $__wasm_call_ctors))
2020
(export "main" (func $main))
2121
(export "__heap_base" (global $global$1))
2222
(export "__data_end" (global $global$2))
@@ -155,4 +155,4 @@
155155
)
156156
)
157157
)
158-
;; METADATA: { "asmConsts": {},"staticBump": 0, "initializers": ["__wasm_call_ctors"], "declares": ["_Z4atoiPKc"], "externs": [], "implementedFunctions": ["___wasm_call_ctors","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory","_dynCall_viii"], "exports": ["memory","__wasm_call_ctors","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory","dynCall_viii"], "invokeFuncs": [] }
158+
;; METADATA: { "asmConsts": {},"staticBump": 0, "initializers": [], "declares": ["_Z4atoiPKc"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory","_dynCall_viii"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory","dynCall_viii"], "invokeFuncs": [] }

0 commit comments

Comments
 (0)