Skip to content

export symbols: support macos/windows(32/64)#155535

Open
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:cstyle-export-symbols
Open

export symbols: support macos/windows(32/64)#155535
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:cstyle-export-symbols

Conversation

@cezarbbb

@cezarbbb cezarbbb commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

View all comments

Previously, in the pr #150992 , export symbols only supported Linux. The special prefix and suffix rules for some symbols in macOS and Windows were not fully supported. This pull request attempts to clarify these rules and add corresponding support.

Currently, the undecorate_c_symbol() function has been added to handle macOS _ prefixes, Windows x86 calling convention modifiers (cdecl/stdcall/fastcall/vectorcall), and Arm64EC # prefixes.

*Update: Handling of Windows C++ mangled symbols has now been added(Linux/macOS don't need).

r? @bjorn3 @petrochenkov

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs 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 Apr 20, 2026
@rustbot

rustbot commented Apr 20, 2026

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: codegen, compiler
  • codegen, compiler expanded to 72 candidates
  • Random selection from 16 candidates

@rustbot rustbot assigned bjorn3 and unassigned wesleywiser Apr 20, 2026
@cezarbbb

Copy link
Copy Markdown
Contributor Author

Could someone pls give me permission to test whether the current modifications will work correctly on Windows and macOS?
Cause I don't have the corresponding devices.

@adwinwhite

Copy link
Copy Markdown
Contributor

@bors delegate=try

@rust-bors

rust-bors Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

✌️ @cezarbbb, you can now perform try builds on this pull request!

You can now post @bors try to start a try build.

@cezarbbb

Copy link
Copy Markdown
Contributor Author

@bors delegate=try

Thanks!!! It helps me a lot:>

@cezarbbb

Copy link
Copy Markdown
Contributor Author

@bors try jobs=x86_64-msvc-*

rust-bors Bot pushed a commit that referenced this pull request Apr 20, 2026
export symbols: support macos/windows(32/64)


try-job: x86_64-msvc-*
@rust-bors

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors rust-bors Bot 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 Apr 20, 2026
@rust-bors

rust-bors Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 22e628f failed: CI. Failed job:

@cezarbbb

Copy link
Copy Markdown
Contributor Author

@bors try jobs=x86_64-msvc-*

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 20, 2026
export symbols: support macos/windows(32/64)


try-job: x86_64-msvc-*
@rust-bors

rust-bors Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 71d836c failed: CI. Failed job:

@rust-log-analyzer

This comment has been minimized.

@cezarbbb

Copy link
Copy Markdown
Contributor Author

@bors try jobs=x86_64-msvc-*

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 20, 2026
export symbols: support macos/windows(32/64)


try-job: x86_64-msvc-*
@rust-bors

rust-bors Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6d8d88e (6d8d88e3ab7025c82dc5ed03d96874614fdf1189, parent: e22c616e4e87914135c1db261a03e0437255335e)

@cezarbbb

Copy link
Copy Markdown
Contributor Author

@bors try jobs=aarch64-*

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 20, 2026
export symbols: support macos/windows(32/64)


try-job: aarch64-*
@cezarbbb

Copy link
Copy Markdown
Contributor Author

@bors try jobs=x86_64-msvc-*

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 22, 2026
export symbols: support macos/windows(32/64)


try-job: x86_64-msvc-*
@rust-bors

rust-bors Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: c0d471b (c0d471b340755a324f088edfb47b158c885cbe75, parent: f9988fefd3add01f414f52b414308e7872622fee)

@cezarbbb

cezarbbb commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

Since #156012 and this PR modified the same test file, after obtaining consent, I merged the fixes from #156012 into this PR and added @jchecahi as a co-author.

@cezarbbb cezarbbb force-pushed the cstyle-export-symbols branch from 1431f40 to 4332ade Compare May 30, 2026 08:00
@rustbot

rustbot commented May 30, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@cezarbbb

cezarbbb commented May 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @bjorn3 for taking the time to review this PR. I've made some optimizations and rebased onto the latest main. Several weeks have passed since the last review, could you please take a look? I look forward to receiving your valuable advice:>

@rustbot

This comment was marked as resolved.

1 similar comment
@rustbot

This comment was marked as resolved.

// Accept both on Windows.
let scope = symbol.scope();
if scope != object::SymbolScope::Dynamic
&& !(sess.target.is_like_windows && scope == object::SymbolScope::Linkage)

@bjorn3 bjorn3 Jun 11, 2026

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.

This should probably check sess.target.binary_format rather than Windows. UEFI also uses COFF.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied.

// Mach-O: strip the leading underscore that all external symbols have.
// The Darwin linker's export_symbols will add it back.
name.strip_prefix('_').map(|s| Cow::Owned(s.to_string())).unwrap_or(Cow::Borrowed(name))
} else if sess.target.is_like_windows {

@bjorn3 bjorn3 Jun 11, 2026

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.

These two should also check sess.target.binary_format.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

if sess.target.is_like_darwin {
// Mach-O: strip the leading underscore that all external symbols have.
// The Darwin linker's export_symbols will add it back.
name.strip_prefix('_').map(|s| Cow::Owned(s.to_string())).unwrap_or(Cow::Borrowed(name))

@bjorn3 bjorn3 Jun 11, 2026

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.

If the name doesn't start with an _, I believe it will not be exported by the linker. In either case returning the original name is wrong as the linker will look for format!("_{name}"), which likely doesn't exist.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The return type of undecorate_c_symbol has been changed to Option<Cow<str>>. Symbols without the _ prefix now return None and are skipped instead of incorrectly returning the original name.

// COFF 32-bit: strip calling-convention decorations.
if let Some(rest) = name.strip_prefix('@') {
// fastcall: @foo@N -> foo
rest.split_once('@')

@bjorn3 bjorn3 Jun 11, 2026

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.

Should these use rsplit_once?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK.

@rustbot rustbot 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 11, 2026
@rustbot

rustbot commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Co-authored-by: Jesus Checa <101630491+jchecahi@users.noreply.github.com>
@cezarbbb cezarbbb force-pushed the cstyle-export-symbols branch from 4332ade to a46ffc1 Compare June 12, 2026 06:19
@cezarbbb

Copy link
Copy Markdown
Contributor Author

@bors try jobs=aarch64-apple

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 12, 2026
export symbols: support macos/windows(32/64)


try-job: aarch64-apple
@rust-bors

rust-bors Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a78f1e6 (a78f1e60ea53757c3ac71ac0b7e9167ed15eeae5, parent: b30f3df3ba3c4c9de2f58f1a75dd9500b79b3f8d)

@cezarbbb

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 12, 2026
@cezarbbb cezarbbb requested a review from bjorn3 June 12, 2026 09:22
fallback: &'a str,
) -> Cow<'a, str> {
if suffix.as_ref().parse::<u32>().is_ok() {
Cow::Owned(base.to_string())

@bjorn3 bjorn3 Jun 12, 2026

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.

Suggested change
Cow::Owned(base.to_string())
Cow::Borrowed(base)

would work too, right? Then strip_numeric_suffix can return &'a str instead.

View changes since the review

strip_numeric_suffix(base, suffix, stripped)
} else {
// cdecl: _foo -> foo
Cow::Owned(stripped.to_string())

@bjorn3 bjorn3 Jun 12, 2026

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.

Suggested change
Cow::Owned(stripped.to_string())
Cow::Borrowed(stripped)

would work, right?

View changes since the review

BinaryFormat::MachO => {
// Mach-O: strip the leading underscore that all external symbols have.
// The Darwin linker's export_symbols will add it back.
name.strip_prefix('_').map(|s| Cow::Owned(s.to_string()))

@bjorn3 bjorn3 Jun 12, 2026

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.

and this, allowing you to remove Cow entirely.

Suggested change
name.strip_prefix('_').map(|s| Cow::Owned(s.to_string()))
name.strip_prefix('_').map(|s| Cow::Borrowed(s))

View changes since the review

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

Labels

A-run-make Area: port run-make Makefiles to rmake.rs O-apple Operating system: Apple / Darwin (macOS, iOS, tvOS, visionOS, watchOS) O-windows Operating system: Windows 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants