Description
Currently, the way export visibility is handled in WASM is that rustc manually passes a list of all exported functions to the WASM linker:
rust/compiler/rustc_codegen_ssa/src/back/linker.rs
Lines 1312 to 1314 in d69c33a
In almost every mode of compilation for Rust it doesn't matter too much if the exports list covers more than is needed, because there's a linker at the very end dealing with this. Rlibs and staticlibs may end up containing extra symbols, but the end binary will have dead code removed by the final linker. Dynamic libraries end up containing extra symbols which is a disk size issue (not a huge deal), but the dynamic linker's capable of only paging in the necessary bits.
The landscape is very different for WASM, however. In WASM, the final binary is a .wasm file that gets sent over the network, and its size matters. It's impractical to compile large libraries to a single .wasm file and recommend everyone use it since most users will not be using 99% of that wasm file.
There are tools for doing dead code elimination in JS, usually going by the name "tree shaking". Furthermore it's often practical for a WASM user to just list out the symbols they need.
With all of this, it would be useful to be able to customize which symbols get exported as a part of the linker process, rather than updating the code (as different library clients will need different things and it might not always be practical to pepper the library with a hundred client specific cfgs, nor is it an appropriate separation of concerns)
It would be nice if rust had a pair of codegen flags that would:
- Allow explicitly allowlisting/listing symbols that get passed down to
rust-lld --export
- Allow explicitly denylisting symbols that get passed down to
rust-lld --export
, to "filter out" features
cc @sffc