-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add support for DllExport on Windows #7196
Comments
It seems that rustc-hash.dll indeed contains exports on Windows, and therefore it must be possible to somehow build such that code is exported. The following method does not produce exports; Crate:
Module:
Thoughts? |
|
Re-opening because the general idea is still sound. Adding a Also, I'm nominating this for production ready, based on the idea that |
Visiting for triage; nothing to add |
Accepted for feature-complete |
Assigning P-low. Not 1.0. |
We actually dllexports all public symbols: // lib.rs
#![crate_type = "dylib"]
#![crate_id = "r"]
#[no_mangle] pub fn func1() {} // exported
#[no_mangle] fn func2() {}
pub fn func3() {} // exported
fn func4() {}
but just by accident! If we don't explicitly export anything, The "unintended export" will not occur if we add some explicit dllexports in native code: // c.c
__declspec(dllexport)
void func5(void) {}
// lib.rs
#![crate_type = "dylib"]
#![crate_id = "r"]
#[link(name = "ext", kind = "static")]
extern "C" {
pub fn func5();
}
#[no_mangle] pub fn func1() {} // exported
#[no_mangle] fn func2() {}
fn func3() { unsafe { func5(); } } // use dllexported symbol
// a.rs
extern crate r;
fn main() {
r::func1();
}
This is bad. We have to export them explicitly in this case. |
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized. This issue has been moved to the RFCs repo: rust-lang/rfcs#826 |
(Note that the missing export behavior is certainly a bug, which can be fixed without introducing dllexport syntax support. I'll open new issue for the one.) |
Note that for MSVC targets the compiler now places I've written up a comment about this, but I'm going to use this bug to track a more proper implementation of using I think this is more concretely actionable in this repo than the RFC repo for now, so I'm going to reopen this. |
I think we shouldn't indiscriminately put |
These two commits are aimed at "fixing" our usage of `dllexport` in the compiler. Currently we blanket apply this attribute to *everything* public in a crate, but this ends up with a few downsides: * Executables are larger than the should be as a result of thinking they should export everything * Native libraries aren't handled correctly because technically a statically included native library should be exported from a DLL in some cases. * Symbols don't actually need to be exported if they never end up in a DLL. The first commit adds a new unstable attribute, `#[linked_from]`, which is a way to tell the compiler what native library a block of symbols comes from. This is used to inform the compiler what set of native libraries are statically included in the rlib (or other output). This information is later used to export them from a DLL if necessary. Currently this is only used in a few places (such as the LLVM bindings) to get the compiler to link correctly. The second commit stops adding `dllexport` to all items in LLVM and instead explicitly telling the linker what symbols should be exported. We only need to do this when building a dynamic library, and otherwise we can avoid adding `dllexport` or telling the linker about exported symbols. As a testament to this change, the size of "Hello World" on MSVC drops from 1.2MB to 67KB as a result of this patch. This is because the linker can much more aggressively remove unused code. These commits do not yet attempt to fix our story with `dllimport`, and I'll leave that to a future PR and issue, for now though I'm going to say that this Closes #7196
It currently does not seem to be possible to export a function from a DLL on Windows. It's not done as one would expect when building with --lib.
The text was updated successfully, but these errors were encountered: