Skip to content

Use the .drectve section for exporting symbols from dlls on Windows#142568

Open
bjorn3 wants to merge 1 commit into
rust-lang:mainfrom
bjorn3:windows_symbols_o_export
Open

Use the .drectve section for exporting symbols from dlls on Windows#142568
bjorn3 wants to merge 1 commit into
rust-lang:mainfrom
bjorn3:windows_symbols_o_export

Conversation

@bjorn3

@bjorn3 bjorn3 commented Jun 16, 2025

Copy link
Copy Markdown
Member

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks @dpaoliello for figuring out this weird quirk of link.exe!

@rustbot

rustbot commented Jun 16, 2025

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot

rustbot commented Jun 16, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@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 16, 2025
@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from e63c76b to 94ec88c Compare June 16, 2025 09:25
Comment thread compiler/rustc_codegen_ssa/src/back/linker.rs Outdated
Comment thread compiler/rustc_codegen_ssa/src/back/linker.rs Outdated
@bjorn3

bjorn3 commented Jun 16, 2025

Copy link
Copy Markdown
Member Author

@bors2 try jobs=x86_64-msvc-1,x86_64-msvc-2

@rust-bors

rust-bors Bot commented Jun 16, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit 5e2efb6 with merge 7bae343

To cancel the try build, run the command @bors2 try cancel.

rust-bors Bot added a commit that referenced this pull request Jun 16, 2025
Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
@rust-bors

rust-bors Bot commented Jun 16, 2025

Copy link
Copy Markdown
Contributor

💔 Test failed

@bjorn3

bjorn3 commented Jun 16, 2025

Copy link
Copy Markdown
Member Author

@bors2 try jobs=x86_64-msvc-1,x86_64-msvc-2

@rust-bors

rust-bors Bot commented Jun 16, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit c353c7d with merge 3dadabc

To cancel the try build, run the command @bors2 try cancel.

rust-bors Bot added a commit that referenced this pull request Jun 16, 2025
Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2

@dpaoliello dpaoliello left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

One overall design question: is it supported for folks to run the linker themselves AND specify their own def file? A couple of my changes broke the Chromium and Fuschia folks, so I'm a little paranoid now about changing anything to do with linking that may affect them.

Comment thread compiler/rustc_codegen_ssa/src/back/linker.rs Outdated
Comment thread compiler/rustc_codegen_ssa/src/back/linker.rs Outdated
@bjorn3

bjorn3 commented Jun 16, 2025

Copy link
Copy Markdown
Member Author

One overall design question: is it supported for folks to run the linker themselves AND specify their own def file?

This PR on it's own has no effect at all on people running the linker themself. Both the .DEF file and the symbols.o file are only generated when rustc itself runs the linker.

As for a future PR that will use weak definitions, those symbols are not meant to be exported from a staticlib anyway. The only case that could break is if someone (Chromium) tries to link rlibs together into a dll and wants to export all symbols, which we don't officially support anyway. The Chromium people have had to adaot their build system for rustc changes anyway and I expect it to not all that hard to adapt to said future weak symbol usage too.

@rust-bors

rust-bors Bot commented Jun 16, 2025

Copy link
Copy Markdown
Contributor

💔 Test failed

@dpaoliello

Copy link
Copy Markdown
Contributor

💔 Test failed

The test executable failed with 0xc0000135 which is STATUS_DLL_NOT_FOUND

Looks like this is a test for shaking out unused functions: is the compiler supposed to do that, or is it relying on the linker?

@bjorn3

bjorn3 commented Jun 17, 2025

Copy link
Copy Markdown
Member Author

@bors2 try jobs=x86_64-msvc-1,x86_64-msvc-2

@rust-bors

rust-bors Bot commented Jun 17, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit 8c88ff4 with merge e97fbdf

To cancel the try build, run the command @bors2 try cancel.

rust-bors Bot added a commit that referenced this pull request Jun 17, 2025
Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from 8c88ff4 to fc88eae Compare June 17, 2025 09:01
@rust-bors

rust-bors Bot commented Jun 17, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: e97fbdf (e97fbdf63e618534ac0929eef0229de9fb477dad, parent: 55d436467c351b56253deeba209ae2553d1c243f)

@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from fc88eae to e6aa201 Compare June 17, 2025 11:41
@bjorn3

bjorn3 commented Jun 17, 2025

Copy link
Copy Markdown
Member Author

Squashed everything and reverted some now unnecessary changes.

@rustbot ready

@wesleywiser

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Jun 30, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit 7539730 has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors 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-review Status: Awaiting review from the assignee but also interested parties. labels Jun 30, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 1, 2025
…=wesleywiser

Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of rust-lang#142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
bors added a commit that referenced this pull request Jul 2, 2025
Rollup of 12 pull requests

Successful merges:

 - #141829 (Specialize sleep_until implementation for unix (except mac))
 - #141847 (Explain `TOCTOU` on the top of `std::fs`, and reference it in functions)
 - #142138 (Add `Vec::into_chunks`)
 - #142321 (Expose elf abi on ppc64 targets)
 - #142568 (Use the .drectve section for exporting symbols from dlls on Windows)
 - #142886 (ci: aarch64-gnu: Stop skipping `panic_abort_doc_tests`)
 - #143038 (avoid suggesting traits from private dependencies)
 - #143194 (fix bitcast of single-element SIMD vectors)
 - #143206 (Align attr fixes)
 - #143258 (Don't recompute `DisambiguatorState` for every RPITIT in trait definition)
 - #143260 (Use the correct export kind for __rust_alloc_error_handler_should_panic)
 - #143274 (ci: support optional jobs)

r? `@ghost`
`@rustbot` modify labels: rollup
@jhpratt

jhpratt commented Jul 2, 2025

Copy link
Copy Markdown
Member

Not certain, but this seems likely to be the cause of #143305 (comment)

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 2, 2025
@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from 7539730 to c16b891 Compare July 3, 2025 11:19
@bjorn3

bjorn3 commented Jul 3, 2025

Copy link
Copy Markdown
Member Author

@bors2 try jobs=dist-i686-msvc

@rust-bors

rust-bors Bot commented Jul 3, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit c16b891 with merge ab158cd

To cancel the try build, run the command @bors2 try cancel.

rust-bors Bot added a commit that referenced this pull request Jul 3, 2025
Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
try-job: dist-i686-msvc
@rust-bors

rust-bors Bot commented Jul 3, 2025

Copy link
Copy Markdown
Contributor

💔 Test failed

@bjorn3

bjorn3 commented Jul 3, 2025

Copy link
Copy Markdown
Member Author

I think def files allow undecorated names, while /export needs the decorated name.

While it would be reasonable to expect the Windows linker to handle
linker args in the .drectve section identical to cli arguments, as it
turns out exporting weak symbols only works when the /EXPORT is in the
.drectve section, not when it is a linker argument or when a .DEF file
is used.
@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from c16b891 to dda12ce Compare July 25, 2025 14:25
@wesleywiser

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Aug 9, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit dda12ce has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors 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 Aug 9, 2025
@bjorn3

bjorn3 commented Aug 9, 2025

Copy link
Copy Markdown
Member Author

@bors r-
@rustbot author

I haven't fixed the CI failure yet.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 9, 2025
@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Aug 9, 2025
@jdonszelmann

Copy link
Copy Markdown
Contributor

hey, I was wondering what the status of this was. I'm sorry if you've just had no time, that's completely understandable.

@bjorn3

bjorn3 commented Aug 25, 2025

Copy link
Copy Markdown
Member Author

It seems that I need to implement symbol mangling for the symbol names that I pass as /export: in .drectve, which requires some refactors to thread through the necessary data like function calling convention and argument sizes to the symbols.o generation location. I haven't had time to work on this yet.

@jdonszelmann

Copy link
Copy Markdown
Contributor

alright :)

@rust-bors

rust-bors Bot commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #152321) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

10 participants