-
Notifications
You must be signed in to change notification settings - Fork 13k
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
export_name works for msvc x86_64 toolchain, but not i686 #44282
Comments
Perhaps try using |
Same deal. I tried a bunch of different combinations and it looks like "\x01" and "@" anywhere in the string trips it up (e.g. "a@b"). Again, this is only on the x86 and not the equivalent x64 toolchain, which seems pretty inexplicable to me, but then I know very little about LLVM. |
Does making it |
I already guessed all sorts of extern types, to no avail. Even if I could trick it into working, it wouldn't explain why the x64 toolchain has no problem with the simpler form. Shouldn't the function compile and link without any changes to its annotations? Are you able to reproduce the problem? I'll make a simple example project if you can't. |
Here, try building this with the two distinct toolchains mentioned in OP: https://github.com/elitak/rust-issue44282 The x86_64 will link and the i686 won't. |
Sorry no I haven't been trying to reproduce locally, just throwing out some suggestions I could think of |
Not even with that demo project? Both toolchains can build it for you?
…On Fri, Sep 8, 2017 at 11:24 AM, Alex Crichton ***@***.***> wrote:
Sorry no I haven't been trying to reproduce locally, just throwing out
some suggestions I could think of
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#44282 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAu8MYAOf0fdYrV_d3itPbCBV553FnQ5ks5sgYZugaJpZM4PLFoN>
.
|
I've experienced this issue before and as far as I can tell it occurs due to the combination of 32-bit name decoration (which 64-bit doesn't have), and the fact that Rust uses a |
This is an old issue but has anyone figured out a solution? I have this issue when compiling something for It seems I'm getting decorations from MSVC on the name. I don't know if that would be fixable from a rust command or if i would need to edit something else. Just for clarification, I've gotten this issue on using |
I never found a workaround, but I also didn't try diving into the generated .def files or anything as was suggested at the end. |
I found a workaround. ordinals.def:
lib.rs: #[export_name = "Java_redacted"]
pub unsafe extern "stdcall" fn Java_redacted(unk1: c_void, unk2: c_void, unk3: c_void, unk4: c_void, unk5: c_void, unk6: c_void, unk7: c_void, unk8: c_void) {
panic!("TODO");
} build.rs: fn main() {
println!("cargo:rustc-cdylib-link-arg=/DEF:./hook/ordinals.def");
} Cargo.toml: [package]
name = "hook"
version = "0.1.0"
edition = "2021"
[dependencies]
...
[lib]
crate-type = ["cdylib"] |
Just stumbled upon this issue on a extern CModeMgr *g_modeMgr; #[export_name = "?g_modeMgr@@3PAVCModeMgr@@A"]
static mut G_MODE_MGR: usize = 0; This will cause the following error:
I was tried to prefix the #[export_name = "\x01?g_modeMgr@@3PAVCModeMgr@@A"]
static mut G_MODE_MGR: usize = 0; |
After doing a deep dive, the issue is pretty multifaceted: This is normally fine, but as per this issue, problems arise whenever it's a MSVC-format C++ mangled symbol that you're trying to export with So, our And in fact, the issue is even worse because C and C++ libraries in MSVC are able to completely omit the leading underscore with a "Why does using
The linker still explodes, because now the
"Why does this not happen in x86_64?" Given these results, there should either be:
|
Came up with a patch for this, though it's definitely hasty. Works though. diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs
index 77c35a1fe7..7de4a897ec 100644
--- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs
+++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs
@@ -572,6 +572,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('#'),
|
…nking, r=davidtwco Fix linking for symbols starting with ? on i686-pc-windows-msvc When using the `export_name` attribute to specifically export a symbol beginning with a question mark on the `i686-pc-windows-msvc` target, that symbol will fail to link and throw a linker error 100% of the time. [Issue writeup.](rust-lang#44282 (comment)) Closes rust-lang#44282 I'm not sure if this is a proper solution, but [LLVM does the same check](https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/Mangler.cpp#L48-L49) which causes this issue, and is applied to [all 32- and 64-bit Windows COFF objects](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/DataLayout.h#L255-L257) (maybe the same patch should be applied for 64 bit windows as well then?). I am *more* unsure of whether this is the proper place for such a solution (and if the exact conditions of is_like_windows are proper for this usecase), or if the underscore should be stripped elsewhere, but it seems like the most correct place. I'm also unsure if there are any backwards compatibility ramifications here. There shouldn't be, because binaries with exported symbols starting with `?` for this target failed to link because of this issue anyway, but still.
…nking, r=davidtwco Fix linking for symbols starting with ? on i686-pc-windows-msvc When using the `export_name` attribute to specifically export a symbol beginning with a question mark on the `i686-pc-windows-msvc` target, that symbol will fail to link and throw a linker error 100% of the time. [Issue writeup.](rust-lang#44282 (comment)) Closes rust-lang#44282 I'm not sure if this is a proper solution, but [LLVM does the same check](https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/Mangler.cpp#L48-L49) which causes this issue, and is applied to [all 32- and 64-bit Windows COFF objects](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/DataLayout.h#L255-L257) (maybe the same patch should be applied for 64 bit windows as well then?). I am *more* unsure of whether this is the proper place for such a solution (and if the exact conditions of is_like_windows are proper for this usecase), or if the underscore should be stripped elsewhere, but it seems like the most correct place. I'm also unsure if there are any backwards compatibility ramifications here. There shouldn't be, because binaries with exported symbols starting with `?` for this target failed to link because of this issue anyway, but still.
…ing, r=<try> Fix linking for symbols starting with ? on i686-pc-windows-msvc When using the `export_name` attribute to specifically export a symbol beginning with a question mark on the `i686-pc-windows-msvc` target, that symbol will fail to link and throw a linker error 100% of the time. [Issue writeup.](rust-lang#44282 (comment)) Closes rust-lang#44282 I'm not sure if this is a proper solution, but [LLVM does the same check](https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/Mangler.cpp#L48-L49) which causes this issue, and is applied to [all 32- and 64-bit Windows COFF objects](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/DataLayout.h#L255-L257) (maybe the same patch should be applied for 64 bit windows as well then?). I am *more* unsure of whether this is the proper place for such a solution (and if the exact conditions of is_like_windows are proper for this usecase), or if the underscore should be stripped elsewhere, but it seems like the most correct place. I'm also unsure if there are any backwards compatibility ramifications here. There shouldn't be, because binaries with exported symbols starting with `?` for this target failed to link because of this issue anyway, but still. try-job: i686-mingw
…ing, r=davidtwco Fix linking for symbols starting with ? on i686-pc-windows-msvc When using the `export_name` attribute to specifically export a symbol beginning with a question mark on the `i686-pc-windows-msvc` target, that symbol will fail to link and throw a linker error 100% of the time. [Issue writeup.](rust-lang#44282 (comment)) Closes rust-lang#44282 I'm not sure if this is a proper solution, but [LLVM does the same check](https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/Mangler.cpp#L48-L49) which causes this issue, and is applied to [all 32- and 64-bit Windows COFF objects](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/DataLayout.h#L255-L257) (maybe the same patch should be applied for 64 bit windows as well then?). I am *more* unsure of whether this is the proper place for such a solution (and if the exact conditions of is_like_windows are proper for this usecase), or if the underscore should be stripped elsewhere, but it seems like the most correct place. I'm also unsure if there are any backwards compatibility ramifications here. There shouldn't be, because binaries with exported symbols starting with `?` for this target failed to link because of this issue anyway, but still. try-job: i686-mingw
I'm trying to build a simple dll and export a function with a pre-mangled name. I have
crate-type
set tocdylib
and my function decorated like so:I'm using MSVC 2017 Community edition v14.10.25017; toggling between these two toolchains using rustup:
nightly-x86_64-pc-windows-msvc unchanged - rustc 1.22.0-nightly (f861b6ee4 2017-09-01)
nightly-i686-pc-windows-msvc unchanged - rustc 1.22.0-nightly (f861b6ee4 2017-09-01)
The former links fine, the latter gives this error:
mydll.dll.exp: error LNK2001: unresolved external symbol "void __cdecl Hook(void)" (?Hook@@YAXXZ)
fatal error LNK1120: 1 unresolved externals
The text was updated successfully, but these errors were encountered: