-
-
Notifications
You must be signed in to change notification settings - Fork 15k
export symbols: support macos/windows(32/64) #155535
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
Open
cezarbbb
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
cezarbbb:cstyle-export-symbols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| mod raw_dylib; | ||||||
|
|
||||||
| use std::borrow::Cow; | ||||||
| use std::collections::BTreeSet; | ||||||
| use std::ffi::OsString; | ||||||
| use std::fs::{File, OpenOptions, read}; | ||||||
|
|
@@ -47,7 +48,7 @@ use rustc_session::{Session, filesearch}; | |||||
| use rustc_span::Symbol; | ||||||
| use rustc_target::spec::crt_objects::CrtObjects; | ||||||
| use rustc_target::spec::{ | ||||||
| BinaryFormat, Cc, CfgAbi, Env, LinkOutputKind, LinkSelfContainedComponents, | ||||||
| Arch, BinaryFormat, Cc, CfgAbi, Env, LinkOutputKind, LinkSelfContainedComponents, | ||||||
| LinkSelfContainedDefault, LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, Os, RelocModel, | ||||||
| RelroLevel, SanitizerSet, SplitDebuginfo, | ||||||
| }; | ||||||
|
|
@@ -2346,6 +2347,79 @@ fn add_rpath_args( | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn strip_numeric_suffix<'a>( | ||||||
| base: &'a str, | ||||||
| suffix: impl AsRef<str>, | ||||||
| fallback: &'a str, | ||||||
| ) -> Cow<'a, str> { | ||||||
| if suffix.as_ref().parse::<u32>().is_ok() { | ||||||
| Cow::Owned(base.to_string()) | ||||||
| } else { | ||||||
| Cow::Borrowed(fallback) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn undecorate_c_symbol<'a>( | ||||||
| name: &'a str, | ||||||
| sess: &Session, | ||||||
| kind: SymbolExportKind, | ||||||
| ) -> Option<Cow<'a, str>> { | ||||||
| match sess.target.binary_format { | ||||||
| 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())) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this, allowing you to remove
Suggested change
|
||||||
| } | ||||||
| BinaryFormat::Coff => { | ||||||
| // MSVC C++ mangled names start with '?' and use a completely different | ||||||
| // decorating scheme that includes '@@' as structural delimiters. | ||||||
| // They must not be subjected to C calling-convention undecoration. | ||||||
| if name.starts_with('?') { | ||||||
| return Some(Cow::Borrowed(name)); | ||||||
| } | ||||||
| Some(match sess.target.arch { | ||||||
| Arch::X86 => { | ||||||
| // COFF 32-bit: strip calling-convention decorations. | ||||||
| if let Some(rest) = name.strip_prefix('@') { | ||||||
| // fastcall: @foo@N -> foo | ||||||
| rest.rsplit_once('@') | ||||||
| .map(|(base, suffix)| strip_numeric_suffix(base, suffix, name)) | ||||||
| .unwrap_or(Cow::Borrowed(name)) | ||||||
| } else if let Some(stripped) = name.strip_prefix('_') { | ||||||
| if let Some((base, suffix)) = stripped.rsplit_once('@') { | ||||||
| // stdcall: _foo@N -> foo | ||||||
| strip_numeric_suffix(base, suffix, stripped) | ||||||
| } else { | ||||||
| // cdecl: _foo -> foo | ||||||
| Cow::Owned(stripped.to_string()) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
would work, right? |
||||||
| } | ||||||
| } else { | ||||||
| // vectorcall: foo@@N -> foo | ||||||
| name.rsplit_once("@@") | ||||||
| .map(|(base, suffix)| strip_numeric_suffix(base, suffix, name)) | ||||||
| .unwrap_or(Cow::Borrowed(name)) | ||||||
| } | ||||||
| } | ||||||
| Arch::X86_64 => { | ||||||
| // COFF 64-bit: vectorcall mangling (foo@@N -> foo) also applies on x86_64. | ||||||
| name.rsplit_once("@@") | ||||||
| .map(|(base, suffix)| strip_numeric_suffix(base, suffix, name)) | ||||||
| .unwrap_or(Cow::Borrowed(name)) | ||||||
| } | ||||||
| Arch::Arm64EC if kind == SymbolExportKind::Text => { | ||||||
| // Arm64EC: `#` prefix distinguishes ARM64EC text symbols from x64 thunks. | ||||||
| name.strip_prefix('#') | ||||||
| .map(|s| Cow::Owned(s.to_string())) | ||||||
| .unwrap_or(Cow::Borrowed(name)) | ||||||
| } | ||||||
| _ => Cow::Borrowed(name), | ||||||
| }) | ||||||
| } | ||||||
| // ELF: no decoration | ||||||
| _ => Some(Cow::Borrowed(name)), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn add_c_staticlib_symbols( | ||||||
| sess: &Session, | ||||||
| lib: &NativeLib, | ||||||
|
|
@@ -2387,7 +2461,14 @@ fn add_c_staticlib_symbols( | |||||
| } | ||||||
|
|
||||||
| for symbol in object.symbols() { | ||||||
| if symbol.scope() != object::SymbolScope::Dynamic { | ||||||
| // The `object` crate returns `Dynamic` for ELF/Mach-O global symbols, | ||||||
| // but always returns `Linkage` for COFF external symbols. | ||||||
| // Accept both for COFF (Windows and UEFI). | ||||||
| let scope = symbol.scope(); | ||||||
| if scope != object::SymbolScope::Dynamic | ||||||
| && !(sess.target.binary_format == BinaryFormat::Coff | ||||||
| && scope == object::SymbolScope::Linkage) | ||||||
| { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -2402,9 +2483,10 @@ fn add_c_staticlib_symbols( | |||||
| _ => continue, | ||||||
| }; | ||||||
|
|
||||||
| // FIXME:The symbol mangle rules are slightly different in Windows(32-bit) and Apple. | ||||||
| // Need to be resolved. | ||||||
| out.push((name.to_string(), export_kind)); | ||||||
| let Some(undecorated) = undecorate_c_symbol(name, sess, export_kind) else { | ||||||
| continue; | ||||||
| }; | ||||||
| out.push((undecorated.into_owned(), export_kind)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would work too, right? Then
strip_numeric_suffixcan return&'a strinstead.View changes since the review