c3lang / c3c

Compiler for the C3 language
https://c3-lang.org
GNU Lesser General Public License v3.0
3k stars 184 forks source link

Compiler support for windows kernel drivers #1425

Open DanyDollaro opened 2 months ago

DanyDollaro commented 2 months ago

Here are some details about generating a windows driver, preliminary note: remember that the windows SDK and WDK are required.

Linked libraries:

Considering an installation with default parameters for the WDK you will be able to find all the static libraries in a path like C:\Program Files (x86)\Windows Kits\<WINDOWS_VERSION>\lib\<WDK_VERSION>\km\<ARCH>\, like “ntoskrnl.lib”, “hal.lib”, etc.

In my current installation in windows 11 the only supported architectures are x64 and ARM64.

Additional libraries:

Due to exploit mitigation Visual studio enforces the usage of the latest spectre mitigation libraries

Compiler/Linker flags

Drivers are characterized by the /SUBSYSTEM:NATIVE linker flag.

The Visual Studio codegen allows you to set the /INTEGRITYCHECK flag, which sets the IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY bit of the DllCharacteristic field into the optional header, this bit is required by some Windows kernel functions such as PsSetCreateProcessNotifyRoutine that would otherwise fail if this bit is not set.

Entry point

The entrypoint of a driver is usually called DriverEntry. Visual studio by default changes the entrypoint of a driver depending on the framework chosen so that it is wrapped with his custom code, for example for WDM and KMDF frameworks the entry points are GsDriverEntry and FxDriverEntry respectively, of course they can be changed.

lerno commented 2 months ago

What about the CRT, what libraries should be linked - presumably it's not the regular win crt? I also wonder how to best indicate that one is doing kernel programming. Possible options:

  1. A special target, e.g. --target winkernel-x64
  2. A setting with the crt, so --wincrt=kernel
  3. Using an attribute @windriver or something
  4. Separate setting: --winkernel=yes