microsoft / windows-drivers-rs

Platform that enables Windows driver development in Rust. Developed by Surface.
Apache License 2.0
1.48k stars 64 forks source link

rust-lld.exe is not compatible with (kernel-mode) driver #182

Open seritools opened 2 months ago

seritools commented 2 months ago

Not sure if this would be in scope for the repo, but would be nice to have documented somewhere.

I wasn't able to get a valid kernel driver binary going. Even the minimal return 0 DriverEntry example resulted in:

PS C:\driver> sc start drivertest
[SC] StartService FAILED 998:

Invalid access to memory location.

It turns out I had a linker override set in my .cargo\config.toml:

[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"

This which works fine for user-mode rust application, but apparently doesn't support all the linker args for kernel drivers correctly.

DropDemBits commented 1 month ago

I believe this is because lld-link (and rust-lld by extension) doesn't discard the .retplne section and instead retains it in the final binary without any attributes set. Merging the section with e.g. INIT or .rdata fixes issue.

You'll also need to manually specify sections as non-pageable since the /kernel flag in lld-link doesn't do anything yet (see https://github.com/DropDemBits/rust-ndis-examples/blob/c09e42f87c749be338305223f7ebf832a3e4d57b/driver/wdf-kmdf-build/src/lib.rs#L43 for an example).