Skip to content

Fix linking for wasm with crate metadata included#158242

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
bjorn3:wasm_rust_dylib_metadata
Jun 22, 2026
Merged

Fix linking for wasm with crate metadata included#158242
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
bjorn3:wasm_rust_dylib_metadata

Conversation

@bjorn3

@bjorn3 bjorn3 commented Jun 22, 2026

Copy link
Copy Markdown
Member

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of #157709, but also helps with #157590.

r? @Mark-Simulacrum

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 22, 2026

@Mark-Simulacrum Mark-Simulacrum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with condition fixed (or justification for why not)

View changes since this review

Comment thread compiler/rustc_codegen_ssa/src/back/linker.rs Outdated
}

let mut symbols = if let CrateType::ProcMacro = crate_type {
exported_symbols_for_proc_macro_crate(tcx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the other part that both wasm-macro PRs implement is altering this -- I altered it to just let symbols get exported per normal rules under -Zwasm-proc-macros, and I think you hardcoded a set of symbols for export (https://github.com/rust-lang/rust/pull/157709/changes#diff-3f87e69f5e21712681d6fca5b6b70542c10eaf49bf48ff16445ddd17f1ae6dadR1876-R1880).

I think that can go in a separate PR though (up to you).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I intentionally didn't do it in this PR as we both need a different set of symbols exported.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my thinking is that we should let the proc macro (expanded) source control symbols, as with normal ~dylibs -- there's a separate pass enforcing that the public API is restricted to proc-macro functions already (that would then get altered a bit under both of our PRs).

But yeah, we can leave it for future work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a separate pass enforcing that the public API is restricted to proc-macro functions already

That doesn't apply to #[no_mangle] in dependencies. That is the reason why this extra code path exists: #99944

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Yeah, that's trickier.

I'm not sure how much we should be worried about extra symbols getting exposed on wasm targets though, since we don't really have symbol conflicts there (right?). Code size is a concern still of course.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 22, 2026
On wasm the crate metadata ends up in a custom section. It is not
possible to refer to create symbols inside a custom section, so
attempting to export it during linking will result in a linker error.
This keeps the target spec flag to deny compiling rust dylibs for wasm,
but may theoretically allow compiling wasm rust dylibs with a custom
target spec. It will help with wasm proc-macros. And in the future we
can try to flip the only-cdylib option to false on wasm.
@bjorn3 bjorn3 force-pushed the wasm_rust_dylib_metadata branch from ab0b54e to 2fa2746 Compare June 22, 2026 14:16
@bjorn3

bjorn3 commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

@bors r=Mark-Simulacrum

@rust-bors

rust-bors Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 2fa2746 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 22, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 22, 2026
…=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of rust-lang#157709, but also helps with rust-lang#157590.

r? @Mark-Simulacrum
rust-bors Bot pushed a commit that referenced this pull request Jun 22, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #155811 (Include AtomicU128/AtomicI128 in docs for any target)
 - #158242 (Fix linking for wasm with crate metadata included)
 - #158119 (Refactor `proc_macro_decls_static`)
 - #158230 (Include `Item::stability` info in rustdoc JSON.)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 22, 2026
…=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of rust-lang#157709, but also helps with rust-lang#157590.

r? @Mark-Simulacrum
rust-bors Bot pushed a commit that referenced this pull request Jun 22, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #155811 (Include AtomicU128/AtomicI128 in docs for any target)
 - #158242 (Fix linking for wasm with crate metadata included)
 - #158119 (Refactor `proc_macro_decls_static`)
 - #158172 (update `asm_experimental_reg` comments)
 - #158230 (Include `Item::stability` info in rustdoc JSON.)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 22, 2026
…=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of rust-lang#157709, but also helps with rust-lang#157590.

r? @Mark-Simulacrum
rust-bors Bot pushed a commit that referenced this pull request Jun 22, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158242 (Fix linking for wasm with crate metadata included)
 - #158119 (Refactor `proc_macro_decls_static`)
 - #158172 (update `asm_experimental_reg` comments)
 - #158230 (Include `Item::stability` info in rustdoc JSON.)
 - #158258 (Use an unexpanded span for actually written down opt out params)
 - #158262 (Change compiler leads in triagebot.toml)
rust-bors Bot pushed a commit that referenced this pull request Jun 22, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #158242 (Fix linking for wasm with crate metadata included)
 - #157978 (Avoid `&raw` recovery ICE after trailing comma)
 - #158119 (Refactor `proc_macro_decls_static`)
 - #158171 (rustdoc: Avoid ICE on unevaluated `type const` projections in array lengths)
 - #158172 (update `asm_experimental_reg` comments)
 - #158230 (Include `Item::stability` info in rustdoc JSON.)
 - #158258 (Use an unexpanded span for actually written down opt out params)
 - #158262 (Change compiler leads in triagebot.toml)
rust-bors Bot pushed a commit that referenced this pull request Jun 22, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #158242 (Fix linking for wasm with crate metadata included)
 - #157978 (Avoid `&raw` recovery ICE after trailing comma)
 - #158119 (Refactor `proc_macro_decls_static`)
 - #158171 (rustdoc: Avoid ICE on unevaluated `type const` projections in array lengths)
 - #158172 (update `asm_experimental_reg` comments)
 - #158230 (Include `Item::stability` info in rustdoc JSON.)
 - #158258 (Use an unexpanded span for actually written down opt out params)
 - #158262 (Change compiler leads in triagebot.toml)
rust-bors Bot pushed a commit that referenced this pull request Jun 22, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #158242 (Fix linking for wasm with crate metadata included)
 - #157978 (Avoid `&raw` recovery ICE after trailing comma)
 - #158119 (Refactor `proc_macro_decls_static`)
 - #158171 (rustdoc: Avoid ICE on unevaluated `type const` projections in array lengths)
 - #158172 (update `asm_experimental_reg` comments)
 - #158230 (Include `Item::stability` info in rustdoc JSON.)
 - #158258 (Use an unexpanded span for actually written down opt out params)
 - #158262 (Change compiler leads in triagebot.toml)
rust-bors Bot pushed a commit that referenced this pull request Jun 22, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #158242 (Fix linking for wasm with crate metadata included)
 - #157978 (Avoid `&raw` recovery ICE after trailing comma)
 - #158119 (Refactor `proc_macro_decls_static`)
 - #158171 (rustdoc: Avoid ICE on unevaluated `type const` projections in array lengths)
 - #158172 (update `asm_experimental_reg` comments)
 - #158230 (Include `Item::stability` info in rustdoc JSON.)
 - #158258 (Use an unexpanded span for actually written down opt out params)
 - #158262 (Change compiler leads in triagebot.toml)
@rust-bors rust-bors Bot merged commit ff81679 into rust-lang:main Jun 22, 2026
13 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 22, 2026
rust-timer added a commit that referenced this pull request Jun 22, 2026
Rollup merge of #158242 - bjorn3:wasm_rust_dylib_metadata, r=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of #157709, but also helps with #157590.

r? @Mark-Simulacrum
@bjorn3 bjorn3 deleted the wasm_rust_dylib_metadata branch June 23, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants