Skip to content
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

Non pub functions are exported as DLL symbols #133437

Open
kobe2000 opened this issue Nov 25, 2024 · 6 comments
Open

Non pub functions are exported as DLL symbols #133437

kobe2000 opened this issue Nov 25, 2024 · 6 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@kobe2000
Copy link

I tried this code:

#[no_mangle]
extern "C" fn foo() {}

I build a Windows DLL, I use this foo function with other static library written in C.

I think that this function should not be exported as DLL symbols, but it does

Meta

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-pc-windows-msvc
release: 1.82.0
LLVM version: 19.1.1
@kobe2000 kobe2000 added the C-bug Category: This is a bug. label Nov 25, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 25, 2024
@workingjubilee
Copy link
Member

why did you attach no_mangle to it?

@ehuss
Copy link
Contributor

ehuss commented Nov 25, 2024

This is the expected behavior of no_mangle (see docs). See #54135 and #54451 for some discussion as to why that is.

@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 25, 2024
@kobe2000
Copy link
Author

why did you attach no_mangle to it?

Without it I can not link the fooo funciton at C side(emcc) due to name mangling

extern "C" void foo();
// #[no_mangle]
extern "C" fn foo() {}

wasm-ld: error: lto.tmp: undefined symbol: foo

@kobe2000
Copy link
Author

This is the expected behavior of no_mangle (see docs). See #54135 and #54451 for some discussion as to why that is.

I read them, but it there any way I can link rust function in static C library without export it?

flowchart
    R(Rust dynamic library) --> C(C static library)
    C -.->|call foo| R
    A(Executable application) --> R
Loading

@ChrisDenton
Copy link
Member

This is the expected behavior of no_mangle (see docs). See #54135 and #54451 for some discussion as to why that is.

Hm, I'm not actually sure that covers DLLs. There is a rationale and documentation for why it must be considered public for staticlibs, rlibs, etc. But this doesn't cover DLLs. E.g. the docs for no_mangle say:

Additionally, the item will be publicly exported from the produced library or object file, similar to the used attribute.

It does not mention the binary at all. And if we look to used for more clarity we see it expressly excludes the final binary:

This attribute forces the compiler to keep the variable in the output object file (.o, .rlib, etc. excluding final binaries) even if the variable is not used, or referenced, by any other item in the crate.

@ultimaweapon
Copy link

You can use .def file to export the symbols with https://learn.microsoft.com/en-us/cpp/build/reference/def-specify-module-definition-file?view=msvc-170. When specify /DEF options the MSVC linker will export only symbols in .def file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

7 participants