You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why are JavaScript inline snippets embedded in wasm data? #9366
Like I expected the JavaScript is put in the support .js file (foo.js in this case)
var ASM_CONSTS=[function($0){foo($0,"supercalif")}];
function _emscripten_asm_const_id(code,a0){
return ASM_CONSTS[code](a0)
}
And disassembling the wasm I see the call to that JavaScript (recompiled with -g3 for the snippet below for the variable names but verified the same code is in the normal version just with short names)
you are right it would be nice if this could be stripped out of the data section of the wasm binary. The reason this is not done, I believe, is because the layout of the static data is all performed by lld and binaryen can't move or remove existing data without breaking data references embedded in the code section. @jgravelle-google am I right about this?
so removing that data section would break your program. Yes that's highly contrived.
The second reason is that like @sbc100 said, we can't actually reclaim that static allocation because any data afterwards would need to be moved down, and by the time we do the EM_ASM magic we no longer have the information needed to do so.
The solution to both of these is to explicitly put the EM_ASM strings in a separate custom data section, and then strip it in binaryen. It's probably slightly more complicated than that because we haven't looked into it in detail, but we'll want to do so one of these days. And also something similar for EM_JS.
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
I wonder if such a custom data section would be able to make external --js-library functions also linkable with same semantics as EM_JS functions currently are?
ooo.. that is kind of an interesting idea. Basically we could compile all .js library files to native object files and let wasm-ld perform all the linking?
I think the one roadblock to this approach would be the fact that JS libraries can include arbirary #if statements so would need to be recompiled whenever settings (or internal settings) change. Effectively re-compiled for each link command. Perhaps we could start by looking into this approach for any library that doesn't contain pre-processor directives?
Activity
sbc100 commentedon Sep 3, 2019
you are right it would be nice if this could be stripped out of the data section of the wasm binary. The reason this is not done, I believe, is because the layout of the static data is all performed by lld and binaryen can't move or remove existing data without breaking data references embedded in the code section. @jgravelle-google am I right about this?
jgravelle-google commentedon Sep 3, 2019
Two bad reasons with a simple fix. First, because we're can't be 100% sure that nobody else is using that string. For example:
would emit something like
so removing that data section would break your program. Yes that's highly contrived.
The second reason is that like @sbc100 said, we can't actually reclaim that static allocation because any data afterwards would need to be moved down, and by the time we do the EM_ASM magic we no longer have the information needed to do so.
The solution to both of these is to explicitly put the EM_ASM strings in a separate custom data section, and then strip it in binaryen. It's probably slightly more complicated than that because we haven't looked into it in detail, but we'll want to do so one of these days. And also something similar for EM_JS.
sbc100 commentedon Sep 3, 2019
Oh.. good idea. I actually think we should do that, if only because its such an elegant fix. I might take a quick look.
sbc100 commentedon Sep 3, 2019
Does EM_JS not share the same problem? (sorry I misread.. I see that you are suggesting fixing that too).
stale commentedon Sep 2, 2020
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
kripken commentedon Sep 2, 2020
It would still be good to fix this.
juj commentedon Nov 15, 2020
I wonder if such a custom data section would be able to make external --js-library functions also linkable with same semantics as EM_JS functions currently are?
sbc100 commentedon Nov 15, 2020
ooo.. that is kind of an interesting idea. Basically we could compile all
.jslibrary files to native object files and let wasm-ld perform all the linking?I think the one roadblock to this approach would be the fact that JS libraries can include arbirary
#ifstatements so would need to be recompiled whenever settings (or internal settings) change. Effectively re-compiled for each link command. Perhaps we could start by looking into this approach for any library that doesn't contain pre-processor directives?Put all EM_JS/EM_ASM strings in a specific data section
Put all EM_JS/EM_ASM strings in a specific data section (#13443)