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

Linking empty rust file fails with "unable to find library -lc-debug" #17191

Open
hoodmane opened this issue Jun 9, 2022 · 9 comments
Open

Comments

@hoodmane
Copy link
Contributor

hoodmane commented Jun 9, 2022

The following:

touch file.rs
rustc file.rs --target wasm32-unknown-emscripten -C link-arg=-sSIDE_MODULE=1 --crate-type cdylib 

Results in
wasm-ld: error: unable to find library -lc-debug.
Rust always includes -sASSERTIONS=1 in the linker args, see rust-lang/rust#97928.

This seems to be a regression between emscripten version 2.0.27 and version 3.1.6. If I surgically set settings.ASSERTIONS=0, then it still fails with "unable to find library -lc". I don't think it should be trying to link -lc anyways because I am trying to make a side module. But I am not sure what the proper setting is to convince Rust or Emscripten to discard this.

@sbc100
Copy link
Collaborator

sbc100 commented Jun 9, 2022

Can you show the failing wasm-ld comment?

I'm pretty sure emscripten will never add -lc or -lc-debug when linking a side module, so I can only imagine that the rust driver is (erroneously) adding it.

I think you best bet is to remove any/all explicit additions of -lc, since the compiler driver (emcc) will add it if/when its needed.

(I don't think this is actually and emscripten bug BTW, but a rustc driver bug)

@hoodmane
Copy link
Contributor Author

hoodmane commented Jun 9, 2022

Yes Rust is adding it. I agree it's probably not an emscripten bug. It's added by Rust in a way that makes it hard to remove, but if I patch emcc to delete -lc I am getting the following:

parse exception: attempted pop from empty stack / beyond block start boundary at 268272 (at 0:268272)]
          Fatal: error in parsing input
          emcc: error: emcc: error: '/home/hood/Documents/programming/emsdk/upstream/bin/wasm-emscripten-finalize --dyncalls-i64 --pass-arg=legalize-js-interface-export-originals --side-module main.wasm -o main.wasm --detect-features'

@sbc100
Copy link
Collaborator

sbc100 commented Jun 9, 2022

That looks like an invalid wasm file... can you gram main.wasm.. does it pass wasm-validate ?

@hoodmane
Copy link
Contributor Author

hoodmane commented Jun 9, 2022

Nope.

@hoodmane
Copy link
Contributor Author

hoodmane commented Jun 9, 2022

Returns a lot of errors like:

main.wasm:00417f0: error: type mismatch in call, expected [i32, i32, i32, i32] but got [i32, i32, i32]
main.wasm:00417f1: error: type mismatch in drop, expected [any] but got []
main.wasm:0041b3f: error: type mismatch in function, expected [] but got [i32, i64]
main.wasm:004217b: error: type mismatch in function, expected [] but got [i32]
main.wasm:00422e4: error: type mismatch in function, expected [] but got [i32]
...

@hoodmane
Copy link
Contributor Author

hoodmane commented Jun 9, 2022

-C lto=thin seems to fix it.

@sbc100
Copy link
Collaborator

sbc100 commented Jun 9, 2022

What is the default ? full LTO?

@sbc100
Copy link
Collaborator

sbc100 commented Jun 9, 2022

Can you see which function is at those offsets? (wasm-objdump -D and then search for the hex offsets)

@hoodmane
Copy link
Contributor Author

hoodmane commented Jun 9, 2022

For the record, to get to this point I have applied the following patch:

diff --git a/emcc.py b/emcc.py
index b97321cde..66b13d744 100755
--- a/emcc.py
+++ b/emcc.py
@@ -3841,6 +3841,9 @@ def process_libraries(state, linker_inputs):
         state.forced_stdlibs.append(native_lib)
       continue
 
+    if lib == "-c":
+      continue
+
     # We don't need to resolve system libraries to absolute paths here, we can just
     # let wasm-ld handle that.  However, we do want to map to the correct variant.
     # For example we map `-lc` to `-lc-mt` if we are building with threading support.
diff --git a/tools/building.py b/tools/building.py
index cb015e18f..7879c868a 100644
--- a/tools/building.py
+++ b/tools/building.py
@@ -360,9 +360,9 @@ def link_lld(args, target, external_symbols=None):
 
   # Emscripten currently expects linkable output (SIDE_MODULE/MAIN_MODULE) to
   # include all archive contents.
-  if settings.LINKABLE and not any(arg.endswith(".rlib") for arg in args):
-    args.insert(0, '--whole-archive')
-    args.append('--no-whole-archive')
 
   if settings.STRICT:
     args.append('--fatal-warnings')

With that patch,

touch file.rs
rustc file.rs --target wasm32-unknown-emscripten -C link-arg=-sSIDE_MODULE=1 --crate-type cdylib -C lto=thin

works and produces a valid empty side module.

hoodmane added a commit to hoodmane/rust that referenced this issue Jun 20, 2022
According to sbc100, when linking with emcc we shouldn't pass -lc:

> your best bet is to remove any/all explicit additions of -lc, since
> the compiler driver (emcc) will add it if/when its needed.
> (I don't think this is an emscripten bug BTW, but a rustc driver bug)

emscripten-core/emscripten#17191 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants