- Feature Name:
raw_dylib_kind
- Start Date: 2019-01-22
- RFC PR: rust-lang/rfcs#2627
- Rust Issue: rust-lang/rust#58713
Summary
Extend#[link]
attribute by addingkind="raw-dylib"
for use on Windows which emits idata sectionsextern
block, so they may be linked against without linking against an import library. Also add a #[link_ordinal]
attribute for specifying
Motivation
Traditionally, to link against a dll, the program must actually link against an import library. For example to depend on some symbols from kernel32.dll
the program links to kernel32.lib
. However, this requires that the correct import libraries be available to link against, and for third party libraries that are only distributed as a dll creating an import library can be quite difficult, especially givenlib.exe
is incapable of creating an import library that links to stdcall
symbols.
A real advantage of this feature, however, is the fact that symbols will be guaranteedkind="raw-dylib"
the user is ensured that the symbol will come from the specified
Sometimes, a crate may know exactlyd3dcompiler.lib
providedkind="raw-dylib"
would allowwinapi
to link to a specific
This would also allowwinapi
to not have to bundle import libraries for the pc-windows-gnu
targets, saving on bandwidth and disk space for users.
Guide-level explanation
When trying to link to a Windows dll, the dylib
kind may sometimes be unsuitable, and kind="raw-dylib"
can be used instead. A central requirement of kind="raw-dylib"
is that the dll has a stable ABI. Here are some examples of validkind="raw-dylib"
:
- You've had it up to here with trying to create an import library for a dll that has
stdcall
functions. - You're in linking hell with multiple複数のimport libraries providing与えるthe same symbol but from different dlls.
- You know exactly正確にwhich dll you need a symbol from, but you don't know which version of the dll the import library is going to give you.
- You maintain
winapi
.
Here is an example of usage:
Some symbols are only exported by ordinal from the dll in which case #[link_ordinal(..)]
may be used:
Reference-level explanation
Add a new attribute #[link_ordinal]
taking#[link_ordinal(116)]
. It can only be specifiedkind="raw-dylib"
.
Add a new possible value raw-dylib
to the kind
propertylink
attribute. When this kind is specified,name
must explicitly
- If
#[link_ordinal]
is specified特定する、指定する、規定するthe idata section節will map from the mangled symbol to the ordinal specified特定する、指定する、規定するin the dll. - If
#[link_name]
is specified特定する、指定する、規定するthe idata section節will map from the mangled symbol to the name specified特定する、指定する、規定するin the dll, without any calling呼び出しconvention decorations added.たすIf calling呼び出しconvention decorations are desired they must be specified特定する、指定する、規定するexplicitly明示的にin the value of the#[link_name]
attribute. - If both
#[link_ordinal]
and#[link_name]
are specified,特定する、指定する、規定するan error will be emitted. - If neither
#[link_ordinal]
nor#[link_name]
are specified,特定する、指定する、規定するthe idata section節will map from the mangled symbol to its unmangled equivalent等価in the dll. The unmangled symbol will not have calling呼び出しconvention decorations. - If
#[no_mangle]
is specified特定する、指定する、規定するan error will be emitted.
The idata section
Drawbacks
Additionalkind
and a new attribute for specifying
Rationale and alternatives代わりのもの、選択肢
The RFC as proposed would allow
No alternatives
Prior art
Many non-native languages
Delphi is a native language
Unresolved questions
Whether there are any unresolved questions is an unresolved question.
Future possibilities
- With the features described記述するin this RFC, we would be one step closer towards a fully完全にstandalone pure Rust target for Windows that does not rely on any external libraries (aside from the obvious and unavoidable runtime dependence on system libraries), allowing許可する、可能にするfor easy installation and easy cross compilation.
- If that were to happen, we'd no longer need to pretend the pc-windows-gnu toolchain is standalone, and we'd be able to stop bundling MinGW bits entirely in favor of the user's own MinGW installation, thereby resolving a bunch of issues such as rust-lang/rust#53454.
- Also with that pure Rust target users would stop complaining about having to install several gigabytes of VC++ just to link their Rust binaries.2進数
- A future extension of this feature would be the ability to optionally lazily load such external functions, since Rust would naturally have all the information required to do so. This would allow許可する、可能にするusers to use functions that may not exist, and be able to write fallback code for older versions.
- Another future extension would be to extend拡張するthis feature to support shared libraries on other platform, as they could also benefit from the ability to be more precise about linking. For example, on Linux and other platforms using ELF shared libraries, the compiler would emit an ELF
NEEDED
entry項目for the specified特定する、指定する、規定するshared library name, and an undefined symbol for each function declared.宣言(On ELF platforms, using thelink_ordinal
attribute would produce産出するan error.) On such platforms, thelink_name
attribute may also specify特定する、指定する、規定するa symbol name that includes a symbol version, including the@@
.- Windows, however, should be the priority and figuring out details of support for other platforms should not block implementation実装and stabilization of this feature on Windows.
- Windows, however, should be the priority and figuring out details of support for other platforms should not block implementation