From 8099510e72e99be83adf43e9f067e280ed8fea03 Mon Sep 17 00:00:00 2001 From: checkraisefold Date: Tue, 24 Sep 2024 17:05:44 -0700 Subject: [PATCH 1/6] Fix linking for symbols starting with ? --- compiler/rustc_codegen_ssa/src/back/symbol_export.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 60ab291935256..25723dd1a5824 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -577,6 +577,7 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>( } let prefix = match &target.arch[..] { + "x86" if target.is_like_windows && undecorated.starts_with("?") => return undecorated, "x86" => Some('_'), "x86_64" => None, "arm64ec" => Some('#'), From 784d47b7d188058e82f465fe4018fcba55041e21 Mon Sep 17 00:00:00 2001 From: checkraisefold Date: Mon, 30 Dec 2024 21:30:29 -0800 Subject: [PATCH 2/6] Add a test --- .../run-make/issue-44282-questionmark-mangle/main.rs | 11 +++++++++++ .../run-make/issue-44282-questionmark-mangle/rmake.rs | 9 +++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/run-make/issue-44282-questionmark-mangle/main.rs create mode 100644 tests/run-make/issue-44282-questionmark-mangle/rmake.rs diff --git a/tests/run-make/issue-44282-questionmark-mangle/main.rs b/tests/run-make/issue-44282-questionmark-mangle/main.rs new file mode 100644 index 0000000000000..d6df6b6133e17 --- /dev/null +++ b/tests/run-make/issue-44282-questionmark-mangle/main.rs @@ -0,0 +1,11 @@ +#![crate_type = "cdylib"] + +#[no_mangle] +pub extern "C" fn foo(a: i32, b: i32) -> i32 { + 1 +} + +#[export_name = "?bar@@YAXXZ"] +pub extern "C" fn bar(a: i32, b: i32) -> i32 { + 2 +} diff --git a/tests/run-make/issue-44282-questionmark-mangle/rmake.rs b/tests/run-make/issue-44282-questionmark-mangle/rmake.rs new file mode 100644 index 0000000000000..2fb91ef736f63 --- /dev/null +++ b/tests/run-make/issue-44282-questionmark-mangle/rmake.rs @@ -0,0 +1,9 @@ +// This test verifies that functions including a leading question mark +// in their export_name attribute successfully compile. +// This is only an issue on Windows 32-bit. + +use run_make_support::{run, run_fail, rustc}; + +fn main() { + rustc().input("main.rs").target("i686-pc-windows-msvc").run(); +} From a4c628c753e9ac5310f045cc6893baf353859158 Mon Sep 17 00:00:00 2001 From: checkraisefold Date: Tue, 31 Dec 2024 00:29:52 -0800 Subject: [PATCH 3/6] Significant test improvements --- .../issue-44282-questionmark-mangle/main.rs | 11 -------- .../issue-44282-questionmark-mangle/rmake.rs | 9 ------- .../symbol-with-question-mark-links.rs | 25 +++++++++++++++++++ 3 files changed, 25 insertions(+), 20 deletions(-) delete mode 100644 tests/run-make/issue-44282-questionmark-mangle/main.rs delete mode 100644 tests/run-make/issue-44282-questionmark-mangle/rmake.rs create mode 100644 tests/ui/symbol-names/symbol-with-question-mark-links.rs diff --git a/tests/run-make/issue-44282-questionmark-mangle/main.rs b/tests/run-make/issue-44282-questionmark-mangle/main.rs deleted file mode 100644 index d6df6b6133e17..0000000000000 --- a/tests/run-make/issue-44282-questionmark-mangle/main.rs +++ /dev/null @@ -1,11 +0,0 @@ -#![crate_type = "cdylib"] - -#[no_mangle] -pub extern "C" fn foo(a: i32, b: i32) -> i32 { - 1 -} - -#[export_name = "?bar@@YAXXZ"] -pub extern "C" fn bar(a: i32, b: i32) -> i32 { - 2 -} diff --git a/tests/run-make/issue-44282-questionmark-mangle/rmake.rs b/tests/run-make/issue-44282-questionmark-mangle/rmake.rs deleted file mode 100644 index 2fb91ef736f63..0000000000000 --- a/tests/run-make/issue-44282-questionmark-mangle/rmake.rs +++ /dev/null @@ -1,9 +0,0 @@ -// This test verifies that functions including a leading question mark -// in their export_name attribute successfully compile. -// This is only an issue on Windows 32-bit. - -use run_make_support::{run, run_fail, rustc}; - -fn main() { - rustc().input("main.rs").target("i686-pc-windows-msvc").run(); -} diff --git a/tests/ui/symbol-names/symbol-with-question-mark-links.rs b/tests/ui/symbol-names/symbol-with-question-mark-links.rs new file mode 100644 index 0000000000000..5d46f0556555c --- /dev/null +++ b/tests/ui/symbol-names/symbol-with-question-mark-links.rs @@ -0,0 +1,25 @@ +// This test ensures functions with an exported name beginning with a question mark +// successfully compile and link. +// +// Regression test for + +//@ build-pass +//@ only-windows +//@ only-x86 +// Reason: This test regards a linker issue which only applies to Windows. +// Specifically, it only occurs due to Windows x86 name decoration, combined with +// a mismatch between LLVM's decoration logic and Rust's (for `lib.def` generation) + +#![crate_type = "cdylib"] + +#[no_mangle] +pub extern "C" fn decorated(a: i32, b: i32) -> i32 { + 1 +} + +// This isn't just `?undecorated` because MSVC's linker fails if the decorated +// symbol is not valid. +#[export_name = "?undecorated@@YAXXZ"] +pub extern "C" fn undecorated(a: i32, b: i32) -> i32 { + 2 +} From f81ebfb485c143594ef69b2496917f6da4eb629e Mon Sep 17 00:00:00 2001 From: checkraisefold Date: Mon, 13 Jan 2025 18:07:19 -0800 Subject: [PATCH 4/6] Match LLVM behavior more closely Co-authored-by: David Wood --- compiler/rustc_codegen_ssa/src/back/symbol_export.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 25723dd1a5824..6dd4617e283e1 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -577,7 +577,7 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>( } let prefix = match &target.arch[..] { - "x86" if target.is_like_windows && undecorated.starts_with("?") => return undecorated, + "x86" | "x86_64" if target.is_like_msvc && undecorated.starts_with("?") => return undecorated, "x86" => Some('_'), "x86_64" => None, "arm64ec" => Some('#'), From bf3633870d4026ea9a39695a0d28b9cd6f20dea5 Mon Sep 17 00:00:00 2001 From: checkraisefold Date: Tue, 14 Jan 2025 01:49:17 -0800 Subject: [PATCH 5/6] Accept tidy changes --- compiler/rustc_codegen_ssa/src/back/symbol_export.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 6dd4617e283e1..043ba97eca907 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -577,7 +577,9 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>( } let prefix = match &target.arch[..] { - "x86" | "x86_64" if target.is_like_msvc && undecorated.starts_with("?") => return undecorated, + "x86" | "x86_64" if target.is_like_msvc && undecorated.starts_with("?") => { + return undecorated + } "x86" => Some('_'), "x86_64" => None, "arm64ec" => Some('#'), From 9c3b53daf581aa0088a39572db4e6e1508f29eb3 Mon Sep 17 00:00:00 2001 From: checkraisefold Date: Tue, 14 Jan 2025 02:19:14 -0800 Subject: [PATCH 6/6] Apply tidy suggestion (2) --- compiler/rustc_codegen_ssa/src/back/symbol_export.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 043ba97eca907..20881093f97b5 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -578,7 +578,7 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>( let prefix = match &target.arch[..] { "x86" | "x86_64" if target.is_like_msvc && undecorated.starts_with("?") => { - return undecorated + return undecorated; } "x86" => Some('_'), "x86_64" => None,