-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Creating shared WebAssembly library #60231
Comments
Why does this need to be done? Locals should always refer to the function's stack frame implicitly. |
I am not referring to locals; the problem is the |
JFTR, same happens with |
Ah I see, that makes more sense |
To reproduce without cargo, I am using this command line:
|
With Relevant bits:
|
Note how |
Since I passed |
Possibly related:
|
FWIW, back in #46645 (comment) (March 2018) I had success with making a relocatable WASM module. Of course it requires a preprocessing bundler / the WASM loader to actually relocate the modules, so it's not as trivial as a PIC module. |
For my use case, I decided that PIC (e.g. importing a global that indicates memory offeset) is more suited than static relocation sections. |
@alexcrichton It does look like #59712 is related. At least passing Do you think there's a way to override relocation mode back to PIC for consumer experimentation with shared Wasm libraries? |
AFAIK no work really has been done to get Rust/wasm working with the dynamic linking proposal. It's pretty highly unlikely that no changes to rustc will be necessary to get things working. I don't personally have time to work on it right now. My guess is that either the upstream proposal will need to change to support a more ELF-like way of compiling shared objects and linking them into a static-like version, a new target will need to be added, or the standard library will need to be recompiled from scratch with xargo/cargo-xbuild and new codegen flags. |
Any progress on this? |
I tried this with rustc 1.46.0-nightly (0c03aee 2020-07-05) just to see if anything changed in the meantime. Currently
If I remove |
Interestingly if I try the C example above: extern int bar(int, char *);
char *s = "Foo";
int foo(int n)
{
return bar(n, s);
} The error is very similar to the one I get from rustc when I try with
This says |
The lld documentation (https://lld.llvm.org/WebAssembly.html) says
So I'm guessing this is something to fix/implement on lld side first before making any changes in rustc? |
Regarding the #![no_main]
#![no_std]
#![feature(link_args, lang_items, core_intrinsics)]
#![allow(unused_attributes)]
#![link_args = "--import-memory"]
#![link_args = "--shared"]
#[panic_handler]
#[no_mangle]
pub fn panic(_info: &::core::panic::PanicInfo) -> ! {
::core::intrinsics::abort();
}
#[lang = "eh_personality"]
extern fn rust_eh_personality() {}
#[link(wasm_import_module = "import")]
extern { pub fn foo(ptr : *const u8); }
#[export_name = "main"]
pub unsafe fn main() {
let x = 5u8;
foo(&x as *const u8);
let hi: [u8; 2] = [b'h', b'i'];
foo(hi.as_ptr());
} I can build this just fine with
The wat: (module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "env" "memory" (memory (;0;) 0))
(import "env" "__indirect_function_table" (table (;0;) 0 funcref))
(import "env" "__stack_pointer" (global (;0;) (mut i32)))
(import "env" "__memory_base" (global (;1;) i32))
(import "env" "__table_base" (global (;2;) i32))
(import "import" "foo" (func $_ZN3lib3foo17hb22c31d0714767f1E (type 0)))
(func $__wasm_call_ctors (type 1)
call $__wasm_apply_relocs)
(func $__wasm_apply_relocs (type 1))
(func $main (type 1)
(local i32)
global.get 0
i32.const 16
i32.sub
local.tee 0
global.set 0
local.get 0
i32.const 5
i32.store8 offset=13
local.get 0
i32.const 13
i32.add
call $_ZN3lib3foo17hb22c31d0714767f1E
local.get 0
i32.const 26984
i32.store16 offset=14 align=1
local.get 0
i32.const 14
i32.add
call $_ZN3lib3foo17hb22c31d0714767f1E
local.get 0
i32.const 16
i32.add
global.set 0)
(export "main" (func $main))) However if I make the array static static HI: [u8; 2] = [b'h', b'i'];
#[export_name = "main"]
pub unsafe fn main() {
let x = 5u8;
foo(&x as *const u8);
foo(HI.as_ptr());
} This fails with the same error. So it seems like there's something to be fixed in handling of static data. The blog post linked above suggests that this is a clang/LLVM bug as they reproduce the problem in a C file. |
Certianly not a clang/LLVM bug. The blog post refers to LLVM-8 (where indeed it didn't work), but since LLVM-9 I can reliably produce shared WebAssembly libraries from C, including static data. And it’s not surpring that this is related to static data: If you don’t have static data, there is nothing to relocate upon linking :-) |
I'm not sure. Here's an example in C: extern void f(char*);
static char *s = "blah";
void test(void) {
f(s);
} Using clang-10:
It's not the exact same error though -- it complains about a |
I just realized that if I use
|
When building Rust simply using the target
This generates a
Now if we look at the code in the
Here the address of the string (static data) is relative to |
Great, that’s all I wanted :-) |
I'm having difficulties building a cdylib with these instructions. I am able to produce a I get something reasonable-looking if I manually extract the |
FWIW instead of this, you can ask Rust to produce the object file directly with |
I am trying to build a shared WebAssembly library (in the sense of https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md) from Rust.
The following looks promising:
If I compile this (using
--target wasm32-unknown-unknown
), I get the following code:It is very promising that this module imports
__stack_pointer
and__memory_base
, and that it puts the static data ("\01gdb_load_rust_pretty_printers.py\00Hi"
) at a position that is determined by the imported__memory_base
.But when the code actually references the
Hi
string, in functionstart
, it simply uses an absolute pointer, without shifting it by__memory_base
:so this does not seem to be working yet.
Is there a flag I am using wrong? Or is this just not yet supported?
The text was updated successfully, but these errors were encountered: