Skip to content

Commit

Permalink
Rollup merge of #84758 - ChrisDenton:dllimport, r=dtolnay
Browse files Browse the repository at this point in the history
MSVC: Avoid using jmp stubs for dll function imports

Windows import libraries contain two symbols for every function: `__imp_FunctionName` and `FunctionName` (where `FunctionName` is the name of the function to be imported).

`__imp_FunctionName` contains the address of the imported function. This will be filled in by the Windows executable loader at runtime. `FunctionName` contains a jmp stub that simply jumps to the address given by `__imp_FunctionName`. E.g. it's a function that solely contains a single jmp instruction:

```asm
jmp __imp_FunctionName
```

When using an external DLL function in Rust, by default the linker will link to FunctionName, causing a bit of indirection at runtime. In Microsoft's C++ it's possible to instead tell it to insert calls to the address in `__imp_FunctionName` by using the  `__declspec(dllimport)` attribute. In Rust it's possible to get effectively the same behaviour using the `#[link]` attribute on `extern` blocks.

----

The second commit also merges multiple `extern` blocks into one block. This is because otherwise Rust will currently create duplicate linker arguments for each block. In this case having duplicates shouldn't matter much other than the noise when displaying the linker command.
  • Loading branch information
Dylan-DPC authored May 23, 2021
2 parents e4ca166 + fc40aa0 commit d5fa533
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 166 deletions.
Loading

0 comments on commit d5fa533

Please sign in to comment.