Skip to content

Functions are mangled even if the link_name attribute is specified #45073

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

Closed
liranringel opened this issue Oct 6, 2017 · 2 comments
Closed
Labels
O-windows Operating system: Windows O-windows-msvc Toolchain: MSVC, Operating system: Windows

Comments

@liranringel
Copy link
Contributor

I'm trying to use bindgen.
When targeting i686-pc-windows-msvc on 64-bit windows host, I noticed that the generated function symbol is mangled even if the link_name attribute is specified. Bindgen already mangles the function and passes it as a value to the link_name attribute, so the function is mangled twice (which is wrong and produce a link error).

For example, with the following header:

int _stdcall foo();

Bindgen will produce:

extern "stdcall" {
    #[link_name = "_foo@0"]
    pub fn foo() -> ::std::os::raw::c_int;
}

But when I use it, I get the following linker error:
error LNK2019: unresolved external symbol __foo@0@0 referenced in function...
As you can see, the function is mangled twice. If I remove the link_name attribute, it works.

@retep998
Copy link
Member

retep998 commented Oct 7, 2017

LLVM automatically performs calling convention decoration on symbols. This sounds like a simple case where bindgen should just not emit link_name, but if you really want to explicitly specify the calling convention decoration yourself then I believe prefixing the name with \x01 should tell LLVM to not automatically decorate symbols.

@liranringel
Copy link
Contributor Author

@retep998 thanks

bors-servo pushed a commit to rust-lang/rust-bindgen that referenced this issue Oct 8, 2017
…ecified, r=emilio

Tell LLVM to not mangle names if they're already mangled

LLVM mangles the name by default but functions are already mangled because the `link_name` attribute's value is mangled.
Prefixing the name with `\u{1}` should tell LLVM to not mangle it.

I originally thought it's a bug in rustc, but it was clarified here: rust-lang/rust#45073
@workingjubilee workingjubilee added O-windows Operating system: Windows O-windows-msvc Toolchain: MSVC, Operating system: Windows labels Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows O-windows-msvc Toolchain: MSVC, Operating system: Windows
Projects
None yet
Development

No branches or pull requests

3 participants