Skip to content

Commit f1d4d65

Browse files
committed
finalize: Strip "em_js" named data segment.
If we find a data segment with the name "em_js", strip it from the binary. Even in debug mode the names given to data segments by clang will always start with ".data." or ".rodata" so there is no risk of normal data ending up in a section with this name. See: emscripten-core/emscripten#13443
1 parent 43d088c commit f1d4d65

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/wasm/wasm-emscripten.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ EmJsWalker fixEmJsFuncsAndReturnWalker(Module& wasm) {
413413
wasm.removeExport(exp.name);
414414
wasm.removeFunction(exp.value);
415415
}
416+
for (auto iter = wasm.memory.segments.begin(); iter != wasm.memory.segments.end(); iter++) {
417+
if (iter->name == "em_js") {
418+
wasm.memory.segments.erase(iter);
419+
break;
420+
}
421+
}
416422
return walker;
417423
}
418424

test/lit/wasm-emscripten-finalize/em_js.wat

+18-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@
66
;; Both functions should be stripped from the binary
77
;; CHECK-NOT: (func
88

9+
;; The data section names "em_js" should be stripped.
10+
;; CHECK-NOT: (i32.const 512) "JS string in named section\00")
11+
12+
;; Other data sections should not be stripped
13+
;; CHECK: (data (i32.const 1024) "some JS string data\00")
14+
;; CHECK: (data (i32.const 2048) "more JS string data\00")
15+
916
;; CHECK: "emJsFuncs": {
10-
;; CHECK-NEXT: "bar": "more JS string dara",
11-
;; CHECK-NEXT: "foo": "some JS string"
17+
;; CHECK-NEXT: "bar": "more JS string data",
18+
;; CHECK-NEXT: "baz": "JS string in named section"
19+
;; CHECK-NEXT: "foo": "some JS string data"
1220
;; CHECK-NEXT: },
1321

1422
(module
1523
(memory 1 1)
16-
(data (i32.const 1024) "some JS string\00")
17-
(data (i32.const 2048) "more JS string dara\00")
24+
(data $em_js (i32.const 512) "JS string in named section\00")
25+
(data (i32.const 1024) "some JS string data\00")
26+
(data (i32.const 2048) "more JS string data\00")
1827
(export "__em_js__foo" (func $__em_js__foo))
1928
(export "__em_js__bar" (func $bar))
29+
(export "__em_js__baz" (func $__em_js__baz))
2030
;; Name matches export name
2131
(func $__em_js__foo (result i32)
2232
(i32.const 1024)
@@ -25,4 +35,8 @@
2535
(func $bar (result i32)
2636
(i32.const 2048)
2737
)
38+
;; Data in "em_js" named section
39+
(func $__em_js__baz (result i32)
40+
(i32.const 512)
41+
)
2842
)

0 commit comments

Comments
 (0)