Skip to content

Commit b5388d7

Browse files
committed
Add documentation for raw-dylib and link_ordinal
1 parent d43000c commit b5388d7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/attributes.md

+3
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ The following is an index of all built-in attributes.
228228
- [`link`] — Specifies a native library to link with an `extern` block.
229229
- [`link_name`] — Specifies the name of the symbol for functions or statics
230230
in an `extern` block.
231+
- [`link_ordinal`] — Specifies the ordinal of the symbol for functions or
232+
statics in an `extern` block.
231233
- [`no_link`] — Prevents linking an extern crate.
232234
- [`repr`] — Controls type layout.
233235
- [`crate_type`] — Specifies the type of crate (library, executable, etc.).
@@ -297,6 +299,7 @@ The following is an index of all built-in attributes.
297299
[`ignore`]: attributes/testing.md#the-ignore-attribute
298300
[`inline`]: attributes/codegen.md#the-inline-attribute
299301
[`link_name`]: items/external-blocks.md#the-link_name-attribute
302+
[`link_ordinal`]: items/external-blocks.md#the-link_ordinal-attribute
300303
[`link_section`]: abi.md#the-link_section-attribute
301304
[`link`]: items/external-blocks.md#the-link-attribute
302305
[`macro_export`]: macros-by-example.md#path-based-scope

src/items/external-blocks.md

+50
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ specifies the kind of library with the following possible values:
122122
- `static` — Indicates a static library.
123123
- `framework` — Indicates a macOS framework. This is only valid for macOS
124124
targets.
125+
- `raw-dylib` - Indicates a dynamic library where the compiler will generate
126+
an import library to link against (see [`dylib` versus `raw-dylib`] below
127+
for details). This is only valid for Windows targets.
125128

126129
The `name` key must be included if `kind` is specified.
127130

@@ -198,6 +201,19 @@ The default for this modifier is `-whole-archive`.
198201
More implementation details about this modifier can be found in
199202
[`whole-archive` documentation for rustc].
200203

204+
#### `dylib` versus `raw-dylib`
205+
206+
On Windows, linking against a dynamic library requires that an import library
207+
is provided to the linker: this is a special static library that declares all
208+
of the symbols exported by the dynamic library in such a way that the linker
209+
knows that they have to be dynamically loaded at runtime.
210+
211+
Specifying `kind = "dylib"` instructs the Rust compiler to link an import
212+
library based on the `name` key, the linker will then use its normal library
213+
resolution logic to find that import library. Alternatively, specifying
214+
`kind = "raw-dylib:` instructs the compiler to generate an import library
215+
during compilation and provide that to the linker instead.
216+
201217
### The `link_name` attribute
202218

203219
The `link_name` attribute may be specified on declarations inside an `extern`
@@ -211,6 +227,39 @@ extern {
211227
}
212228
```
213229

230+
Using this attribute with the `link_ordinal` attribute will result in a
231+
compiler error.
232+
233+
### The `link_ordinal` attribute
234+
235+
This attribute is only used with the `raw-dylib` linking kind.
236+
It is ignored if used with any other kind.
237+
238+
On Windows, symbols exported from a dynamic library can either be found using
239+
their name or by a unique number call an "ordinal". The `link_ordinal`
240+
attribute can be applied on declarations inside an `extern` block to indicate
241+
the ordinal to use when generating the import library to link against.
242+
243+
<div class="warning">
244+
245+
Warning: `link_ordinal` should only be used in cases where the ordinal of the
246+
symbol is known to be stable: if the ordinal of a symbol is not explicitly set
247+
when its containing binary is built then one will be automatically assigned to
248+
it, and that assigned ordinal may change between builds of the binary.
249+
250+
</div>
251+
252+
```rust
253+
#[link(name = "exporter", kind = "raw-dylib")]
254+
extern "stdcall" {
255+
#[link_ordinal(15)]
256+
fn imported_function_stdcall(i: i32);
257+
}
258+
```
259+
260+
Using this attribute with the `link_name` attribute will result in a
261+
compiler error.
262+
214263
### Attributes on function parameters
215264

216265
Attributes on extern function parameters follow the same rules and
@@ -233,3 +282,4 @@ restrictions as [regular function parameters].
233282
[regular function parameters]: functions.md#attributes-on-function-parameters
234283
[`bundle` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-bundle
235284
[`whole-archive` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-whole-archive
285+
[`dylib` versus `raw-dylib`]: #dylib-versus-raw-dylib

0 commit comments

Comments
 (0)