Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR_ON_UNDEFINED_SYMBOLS=1 always passed for emcc effectively prevents any dynamic linking #41750

Closed
NikVolf opened this issue May 4, 2017 · 10 comments
Labels
C-bug Category: This is a bug. O-asmjs Target: asm.js - http://asmjs.org/ O-wasm Target: WASM (WebAssembly), http://webassembly.org/

Comments

@NikVolf
Copy link
Member

NikVolf commented May 4, 2017

the following rust code is failing to compile using wasm32-unknown-emscripten target

extern {
  #[link(name="env")]
  fn log_event(id: *const u8);
}

fn main() {
    unsafe { log_event(::std::ptr::null()); }
}

with

  error: unresolved symbol: log_event

correct me if I'm wrong, I might be terrible wrong with this, but #[link] without kind=static implies dynamic linking, and the above code should not fail to compile?

As for emscripten itself, it has no problem compiling C code like this:

extern void log_event(void* ptr);

int main() {
    log_event(0);
}

with emcc <source above.c> -O3 -s WASM=1 -s SIDE_MODULE=1 -o result.wasm
producing nice import entry in the result.wasm

  (import "env" "_log_event" (func (;0;) (type 0)))

So, the question is, is there is any option to have dynamic symbols in the resulting wasm? Or at least override this linker ERROR_ON_UNDEFINED_SYMBOLS argument at some point?

source link would be:

"ERROR_ON_UNDEFINED_SYMBOLS=1".to_string()]);

@Mark-Simulacrum Mark-Simulacrum added O-asmjs Target: asm.js - http://asmjs.org/ O-wasm Target: WASM (WebAssembly), http://webassembly.org/ labels Jun 22, 2017
@pepyakin
Copy link
Contributor

pepyakin commented Jul 2, 2017

@NikVolf actually you can override emcc arguments with help of EMMAKEN_CFLAGS environment variable.
(assuming your filename is main.rs)

export EMMAKEN_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" 
rustc --target=wasm32-unknown-emscripten main.rs

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 27, 2017
@tmarkovski
Copy link

tmarkovski commented Nov 15, 2021

@NikVolf actually you can override emcc arguments with help of EMMAKEN_CFLAGS environment variable. (assuming your filename is main.rs)

export EMMAKEN_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" 
rustc --target=wasm32-unknown-emscripten main.rs

Thank you for this, it saved me so much time.
I have previously tried to override this in the .cargo/config file by adding

[target.wasm32-unknown-emscripten]
rustflags = [
    "-C", "link-args=--no-entry",
    "-C", "link-args=-s",
    "-C", "link-args=ERROR_ON_UNDEFINED_SYMBOLS=0" ]

but they ended up being overriden.

@nedv-eu
Copy link

nedv-eu commented Nov 16, 2021

As far as I know wasm32 targets by default do not support dynamic linking as there is no dynamic linker available on the platform. Ignoring error on undefined symbols may lead to hard to find runtime errors. This is a chapter from emscripten doc about dynamic linking, are you sure you really want to use the experimental dynamic linking?

Dynamic linking

Emscripten’s goal is to generate the fastest and smallest possible code, and for that reason it focuses on generating a single JavaScript file for an entire project. For that reason, dynamic linking should be avoided when possible.

By default, Emscripten .so files are the same as regular .o object files. Dynamic libraries that you specify in the final build stage (when generating JavaScript or HTML) are linked in as static libraries. Emcc ignores commands to dynamically link libraries during the compile stage (i.e., not in the final build stage). This is to ensure that the same dynamic library is not linked multiple times in intermediate build stages, which would result in duplicate symbol errors.

There is experimental support for true dynamic libraries, loaded as runtime, either via dlopen or as a shared library. See that link [https://github.com/emscripten-core/emscripten/wiki/Linking] for the details and limitations.

@hoodmane
Copy link
Contributor

hoodmane commented Jun 15, 2022

I think this issue has been fixed in Emscripten. Anything you want to add @sbc100?

@sbc100
Copy link

sbc100 commented Jun 15, 2022

-sERROR_ON_UNDEFINED_SYMBOLS=1 should not be needed anymore. Its on my default when it makes sense to be these days. I believe it was also recently removed from the rust toolchain so I think this issue can be closes yes.

@hoodmane
Copy link
Contributor

The author asserts that there is an incompatibility between -sSIDE_MODULE and -sERROR_ON_UNDEFINED_SYMBOLS which I think at least hasn't been true recently (though they are kind of contradictory).

@sbc100
Copy link

sbc100 commented Jun 15, 2022

Its true that most users don't want -sERROR_ON_UNDEFINED_SYMBOLS=1 in combination with -sSIDE_MODULE.

But emcc will take care of setting the default value of ERROR_ON_UNDEFINED_SYMBOLS correctly in both cases. So simply not specifying this option does the right thing in most cases, which is why I recommended removing this from the default set of rustc options:

https://github.com/emscripten-core/emscripten/blob/f677b2672fa0d3a216068feb0f5ab76192c7d75d/emcc.py#L2101-L2105

@hoodmane
Copy link
Contributor

Okay so while we are using the current stable Rust which does explicitly invoke emcc with -sERROR_ON_UNDEFINED_SYMBOLS=1 we may need something like EMMAKEN_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0" .

@sbc100
Copy link

sbc100 commented Jun 15, 2022

Indeed. But I guess this bug should still be marked as fixed? (since it is now fixed as HEAD, and no further action is planned/needed).

@workingjubilee
Copy link
Member

Thank you! This does indeed seem to be fixed, or if there is a problem still, it probably is better covered by another issue, so I am closing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-asmjs Target: asm.js - http://asmjs.org/ O-wasm Target: WASM (WebAssembly), http://webassembly.org/
Projects
None yet
Development

No branches or pull requests

8 participants