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

wasm-ld: error: test.o: relocation R_WASM_MEMORY_ADDR_LEB cannot be used against symbol version_getter; recompile with -fPIC #42738

Closed
nomeata mannequin opened this issue Sep 21, 2019 · 14 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla lld:wasm

Comments

@nomeata
Copy link
Mannequin

nomeata mannequin commented Sep 21, 2019

Bugzilla Link 43393
Version 12.0
OS Linux

Extended Description

This used to work at revision 358327, but seems to have stopped working with the 9 release. Consider the file (test.c:

int get_version() { return 0; }
int (*version_getter)() = &get_version;
attribute ((visibility("default"))) int version() { return (*version_getter)(); }

Compiling this with

clang-9 --compile -fpic -fvisibility=hidden --target=wasm32-unknown-unknown-wasm -fno-builtin -ffreestanding test.c -o test.o
wasm-ld -o test.wasm --import-memory --shared --no-entry --gc-sections --export=__wasm_call_ctors test.o

yields

wasm-ld: error: test.o: relocation R_WASM_MEMORY_ADDR_LEB cannot be used against symbol version_getter; recompile with -fPIC

But note that I am compiling it with -fPIC (and it used to work).

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented Oct 17, 2019

Mild ping, if that’s ok? It would at least be good to at least know if this should work, or if it is unsupported.

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented Oct 19, 2019

Just checked with latest revision 375226, and the problem still exists.

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented Oct 21, 2019

The problem seems to be with clang, not lld:

If I build with latest clang, but then try to link with wasm-ld from 358327 (the version that used to work), I still get that error.

If I build witht the old clang, I can link with the new wasm-ld, it works.

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented Oct 21, 2019

Reading a bit through the git log, maybe https://reviews.llvm.org/D61539 by Sam Clegg might be related, or any of these;

git lg fc59a03..HEAD -G R_WASM_MEMORY_ADDR_LEB

  • 9abe8c4 - [lld][WebAssembly] Report undefined symbols during scanRelocations (vor 3 Monaten)
  • d0e1d00 - [lld][WebAssembly] Fix typo in error message (vor 4 Monaten)
  • cadcb9e - [WebAssembly] Fix list of relocations with addends in lld (vor 4 Monaten)
  • 8fcf012 - [WebAssembly] Refactor synthetic sections and relocation processing. NFC. (vor 5 Monaten)
  • 0b13ca2 - [WebAssembly] Refactor relocation processing. NFC. (vor 5 Monaten)
  • 08fa01a - Revert "[WebAssembly] Don't generate unused table entries." (vor 6 Monaten)
  • b33fdb7 - [WebAssembly] Don't generate unused table entries. (vor 6 Monaten)
  • 7cdec27 - [WebAssembly] Error on relocations against undefined data symbols. (vor 6 Monaten)

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented Nov 4, 2019

Still present in 65f61c0
(there was some possibly related change in https://reviews.llvm.org/D69600 so I was optimistic)

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented Nov 6, 2019

I did a git bisect (yay for moder tools), and found the commit that broke this for me:

commit 53572d0
Author: Dan Gohman dan433584@gmail.com
Date: Wed Jun 5 20:01:01 2019 +0000

[WebAssembly] Limit PIC support to the Emscripten target

The current PIC support currently only works with Emscripten, so
disable it for other targets.

This is the PIC portion of https://reviews.llvm.org/D62542.

Reviewed By: dschuff, sbc100

llvm-svn: 362638

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented Nov 6, 2019

Ha, I guess I can just use --target=wasm32-unknown-emscripten-wasm instead of --target=wasm32-unknown-unknown-wasm and things works. Not sure what the implications of this are, but it seems to unblock me for now.

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented May 19, 2021

Hmm, this worked for a while (llvm10, using --target=wasm32-emscripten), but after an upgrade of nixpkgs, it stopped working again.

Unfortunately, I am a bit out of my depth here.

@nomeata
Copy link
Mannequin Author

nomeata mannequin commented May 19, 2021

Hmm, maybe it stopped working when using wasi instead of emscripten in the target?

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@km19809
Copy link

km19809 commented May 3, 2022

Is there any update? I'm suffering a similar issue.

@llvmbot
Copy link
Member

llvmbot commented May 3, 2022

@llvm/issue-subscribers-lld-wasm

@sbc100 sbc100 self-assigned this May 3, 2022
@sbc100
Copy link
Collaborator

sbc100 commented May 3, 2022

Simplified example:

$ cat test.c
int (*func_ptr)();

int version() {
  return func_ptr();
}
$ clang -c -fPIC -fvisibility=default --target=wasm32-unknown-emscripten test.c 
$ wasm-ld -o test.wasm --shared test.o 
wasm-ld: warning: creating shared libraries, with -shared, is not yet stable
wasm-ld: error: test.o: relocation R_WASM_MEMORY_ADDR_LEB cannot be used against symbol `func_ptr`; recompile with -fPIC

The problem only occurs when compiling with wasm32-unknown-unknown. With wasm32-unknown-emscripten it works as expected:

$ clang -c -fPIC -fvisibility=default --target=wasm32-unknown-emscripten test.c -o test.o
$ wasm-ld -o test.wasm --shared test.o
wasm-ld: warning: creating shared libraries, with -shared, is not yet stable

As the warnings say the PIC ABI is somewhat experimental and that only place it is know to actually work and get tested is within emscripten.

@sbc100
Copy link
Collaborator

sbc100 commented May 3, 2022

I think this can be closed as expected behaviour, but can you first confirm that using the emscripten triple with ToT works for you?

@km19809
Copy link

km19809 commented May 4, 2022

@sbc100 I tested your emscripten triplet example, then It compiled successfully.
That means my problem does not come from this issue.
As I think, it can be closed now.
Thank you.

@sbc100 sbc100 closed this as completed May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla lld:wasm
Projects
None yet
Development

No branches or pull requests

4 participants