-
Notifications
You must be signed in to change notification settings - Fork 691
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
wat2wasm fails with call_indirect and relocation #1199
Comments
Looks like it's not supported. I added an error message for now, so at least it won't crash: #1202. |
ok, thanks... any plans for the implementation? |
For sure we should implement this. Out of interest what is your use case for the |
i'm adding wasm target to freepascal compiler (https://github.com/skalogryz/freepascal/tree/webasm) (https://lists.freepascal.org/pipermail/fpc-pascal/2019-September/056848.html) The compiler internals make it hard to use tree-structure code of binaryen's the pascal is modular. Each compiled wasm object file needs to be linked in the end. as for |
I see. So your compiler outputs Sounds like you are the first real user of @aardappel recently added support for his lobster language, and as part of the he made a language agnostive C++ library (a header-only library I believe) that can be used to directly generate relocatable wasm object files. @aardappel would this a good use for your library? If you use that you wouldn't need wabt at all I think. |
so far, automatically generated symbols worked fine.
a disclaimer to use wabt? :) |
Maybe you don't need data relocations in pascal? But this kind of relocation is currently not representable in wat format (at global scope):
In a C toolchain this would generate 8 bytes of static data, the with a relocation (pointing to the symbol bar) in the second 4 bytes. I'm happy to continue to support and improve |
@sbc100 looks like the FreePascal compiler is itself written in Pascal, so wholesale adoption of my C++ library is unlikely to work :) @skalogryz that said, if you want to learn how to output linkable wasm object files that work with LLD, this C++ header is a small amount of self-contained code that writes such a binary, in particular see the function |
@aardappel thanks. I've actually already started a binary utility myself (https://github.com/skalogryz/wasmbin). As it was mentioned earlier there's no much over the symbol information through the FPC does allow support of internal object file writers. With wasm object format being relatively simple I was hoping to avoid going that path. (adding a compiler target is not the same as maintaining the target). It's more fun to use the new (wasm) features (i.e. threads, exceptions.. etc), rather than keeping implementing them (once they become the part of the standard) :) |
No, they don't. The problem is that effective address of |
on the topic of per this document: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md#merging-function-sections
I don't think that follows WebAssembly text format convention. (the only argument of |
But what you are describing is exactly what the linker is supposed to do. That is what relocations are for. |
Right, this is why the
The assembler then creates a relocation after the |
In short, the |
@sbc100 - I'm using WAT as a first-class assembly language for a fantasy console. There's a runtime with a memory-mapped API, but the user writes all of their code in WAT, and any of their modules can import whatever any other module exports. Do you foresee a problem with that? |
The issue we are discussing here mostly refers to linking with other object files. if you are not using a linker then writing pure WAT is a lot more reasonable. Once the linker gets involved you will, at a minimum, want to assign symbolic names to memory locations. |
Thank you, @sbc100. Much appreciated. |
Agreed that the wat format doesn't support this well. However, since we have the |
Relocations for call_indirect (and func types in general) are being implemented in #1525 |
@Sammax unfortunately, we can't really add text syntax like that, since wabt tries to match the spec (and future proposals). The best we can hope for is using annotations for this, perhaps something like:
Memory addresses would require a similar annotation syntax. The wat-numeric-values proposal will help here too. |
Do you think a spec proposal to add named data segments and to resolve data segment names to their addresses would have any chance of being accepted? Annotations would work too I guess, though I think we should keep them independent of relocations, since it would be useful to refer to memory locations by name even in non-relocated code. |
Maybe a proposal like that would be accepted. I think the big question is what is the expected output from a module using this text format. If we're saying that it creates relocation entries in the linking sectio, then I think that may be a problem, since the linking section format is not standardized (at least as far as the spec is concerned). We could say that it is like an annotation and can be interpreted as desired by the tools. But that seems a little unsatisfying too.
It's a little tricky to make that work, since data segments don't require a fixed offset. For example, you can write:
If we extend the syntax to allow named memory locations, then there's no way for that to be a simple |
I had been thinking that in normal mode it would work the same as in normal assembly files, i.e. just resolve to the address, and in relocatable mode it would additionally add the relocations. But that doesn't really work with data segments that use a non-const offset. Not allowing referring to such segments might work, or maybe not allowing naming such segments. We could also introduce some kind of pseudo instruction |
Agreed, I don't think we'll want pseudo-instructions in the wat format. I guess I can image something like this though: (data (i32.const 0)
$label1
"some data"
$label2
"some more data"
...
)
...
(i32.const data-addr=$label2)
I'm not sure about how this works, @sbc100 would know. |
The linker assumes that it has ultimate control over the offset of all data segments. That is, I think it completely ignores the offsets specified in the input files. Since the linker decides the data layout, all data references require relocations. |
@binji I like that syntax, I think we would also need
@sbc100 That is the case for both const and global offsets, but not for passive ones, right? I think we could have labels/names for global-offset segments in a way that makes sense in non-relocatable mode too by making them resolve to an offset relative to the segment:
In this example,
This would only happen when the offset is a global, for const offsets the label would resolve to an absolute address calculated as |
Cool! Easiest way to start is to open an issue in WebAssembly/design with a little background, and a proposed syntax. Then we can discuss it a little bit there before bringing it to the group. This would be a similar kind of proposal to WebAssembly/design#1348, since they both only affect the text format. So you could use that as inspiration. |
Thanks, I'll take a look at that! |
Fixed in #1525 |
The following code fails to compile by wat2wasm (v1.12) if relocatable binary (-r) is requested.
Is it expected? (exit code 0xC0000005)
Non-relocatable binary compiles just fine.
The text was updated successfully, but these errors were encountered: