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

Support for emitting custom sections #30

Closed
koute opened this issue Jan 19, 2018 · 8 comments
Closed

Support for emitting custom sections #30

koute opened this issue Jan 19, 2018 · 8 comments

Comments

@koute
Copy link

koute commented Jan 19, 2018

There should be a way to emit custom wasm sections from inside Rust code.

There are many potential uses for custom sections. For example, currently the the js! macro in stdweb takes JavaScript code, stringifies it, and assigns it to a normal constant. Then cargo-web scans the .wasm file, extracts those JavaScript snippets, and deletes the (now useless) corresponding data section entries.

Ideally instead of mucking around with the data section (which often creates holes in memory at runtime since you can't really move the entries' offsets around, and is harder to process) it'd just read a custom section which stdweb would emit for it, and then easily nuke it afterwards.

One potential way we could support custom sections would be like this: have a built-in macro, say, append_to_custom_section!, which would take two arguments: (1) a string with the name of the section, (2) a blob of bytes it would append to that section. It would also be a purely compile-time construct and would expand into nothing after macro expansion.

For example this Rust code:

append_to_custom_section!("js-snippets", b"\x12\x00\x00\x00console.log('...')")
append_to_custom_section!("js-snippets", b"\x0c\x00\x00\x00alert('...')")

would result in a custom wasm section with the name js-snippets containing \x12\x00\x00\x00console.log('...')\x0c\x00\x00\x00alert('...'). The blobs of bytes would be concatenated in the order of their appearance within one .rs file, and with an unspecified order between different .rs files.

This would allow a procedural macro to put arbitrary information in a custom section of its choice, which then could be easily consumed by external tools.

@pepyakin
Copy link
Member

I doubt that LLVM can emit custom sections. Maybe @sunfishcode knows better.

@sunfishcode
Copy link

It can't currently. It would be possible to add ways to do it. Another option would be to have rustc just append custom sections to the output wasm files itself after LLVM is done.

@pepyakin
Copy link
Member

Ah, I completly forgot about it is so easy to just slap a section to a wasm file. I actually like this approach!

@Diggsey
Copy link

Diggsey commented Jan 20, 2018

Sounds good!

@sunfishcode
Copy link

There's a patch for this in review upstream: https://reviews.llvm.org/D43097

@alexcrichton
Copy link
Contributor

While we wait for support in LLVM/LLD (it'd take us awhile to update LLVM as well) I've opened rust-lang/rust#48883 to implement this

@alexcrichton
Copy link
Contributor

This is now in nightly!

@IAIAE
Copy link

IAIAE commented Jul 25, 2022

can rust code "read" the custom section in which the wasm file build by itself? @koute

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

6 participants