diff options
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Makefile | 103 | ||||
-rw-r--r-- | README.md | 34 | ||||
-rw-r--r-- | ddk-template-cplusplus.cpp | 50 | ||||
-rw-r--r-- | ddk-template.c | 25 | ||||
m--------- | mingw-w64-build | 11 |
6 files changed, 226 insertions, 0 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..0040b05 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "mingw-w64-build"] + path = mingw-w64-build + url = https://github.com/Zeranoe/mingw-w64-build.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e4011a6 --- /dev/null +++ b/Makefile @@ -0,0 +1,103 @@ +LOCAL_MINGW64_BUILD_SCRIPT := ./mingw-w64-build/mingw-w64-build +LOCAL_MINGW64_BUILD_DIR := ./x86_64-w64-mingw32 +LOCAL_MINGW64_CC := $(LOCAL_MINGW64_BUILD_DIR)/bin/x86_64-w64-mingw32-gcc +LOCAL_MINGW64_DDK_INCLUDE_DIR := $(LOCAL_MINGW64_BUILD_DIR)/x86_64-w64-mingw32/include/ddk + +CC = $(LOCAL_MINGW64_CC) +CXX = $(dir $(CC))/x86_64-w64-mingw32-g++ +DDK_INCLUDE_DIR = $(LOCAL_MINGW64_DDK_INCLUDE_DIR) +CFLAGS = -Wall -m64 -shared \ + -I$(DDK_INCLUDE_DIR) \ + -D__INTRINSIC_DEFINED_InterlockedBitTestAndSet \ + -D__INTRINSIC_DEFINED_InterlockedBitTestAndReset + +1_DRIVER_NAME = ddk-template +1_OBJECTS = $(1_DRIVER_NAME).o +1_TARGET = $(1_DRIVER_NAME).sys + +2_DRIVER_NAME = ddk-template-cplusplus +2_OBJECTS = $(2_DRIVER_NAME).opp +2_TARGET = $(2_DRIVER_NAME).sys + +all: deps-print-local-notice check-vars $(1_TARGET) $(2_TARGET) + +deps-print-local-notice: +ifeq ($(CC),$(LOCAL_MINGW64_CC)) +ifeq ($(DDK_INCLUDE_DIR),$(LOCAL_MINGW64_DDK_INCLUDE_DIR)) + @echo + @echo "--------------------------------------------------------" + @echo "-- You did not set CC and DDK_INCLUDE_DIR explicitly! --" + @echo "--------------------------------------------------------" + @echo "Using defaults:" + @echo "\tCC=$(CC)" + @echo "\tDDK_INCLUDE_DIR=$(DDK_INCLUDE_DIR)" + @echo +endif +endif + +check-vars: +ifeq ($(CC),$(LOCAL_MINGW64_CC)) +ifneq ($(DDK_INCLUDE_DIR),$(LOCAL_MINGW64_DDK_INCLUDE_DIR)) + @echo + @echo "------------------------------------------------------------------------" + @echo "-- You did not set CC explicitly but set the mingw64 ddk include dir. --" + @echo "------------------------------------------------------------------------" + @echo "\tCC=$(CC)" + @echo "\tDDK_INCLUDE_DIR=$(DDK_INCLUDE_DIR)" + @echo + @echo "This is not supported!" + @echo + @false +endif +endif + +$(LOCAL_MINGW64_BUILD_SCRIPT): +ifeq ($(CC),$(LOCAL_MINGW64_CC)) +ifeq ($(DDK_INCLUDE_DIR),$(LOCAL_MINGW64_DDK_INCLUDE_DIR)) + @echo + @echo "------------------------------------------------------------------------------" + @echo "-- ./mingw-w64-build/mingw-w64-build does not exist, clonging git submodule --" + @echo "------------------------------------------------------------------------------" + @echo + git submodule update --init +endif +endif + +$(LOCAL_MINGW64_CC): +ifeq ($(CC),$(LOCAL_MINGW64_CC)) +ifeq ($(DDK_INCLUDE_DIR),$(LOCAL_MINGW64_DDK_INCLUDE_DIR)) + @echo + @echo "----------------------------------------------------------------------------------------" + @echo "-- ./x86_64-w64-mingw32/bin/x86_64-w64-mingw32-gcc does not exist, building toolchain --" + @echo "----------------------------------------------------------------------------------------" + @echo + ./mingw-w64-build/mingw-w64-build x86_64 +endif +endif + +.deps-built: $(LOCAL_MINGW64_BUILD_SCRIPT) $(LOCAL_MINGW64_CC) + touch .deps-built + +deps: .deps-built + +clean: + rm -f $(1_OBJECTS) $(1_TARGET) + rm -f $(2_OBJECTS) $(2_TARGET) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +%.opp: %.cpp + $(CXX) $(CFLAGS) -c $< -o $@ + +$(1_TARGET): .deps-built $(1_OBJECTS) + $(CC) -std=c99 $(CFLAGS) -Wl,--subsystem,native -Wl,--image-base,0x140000000 -Wl,--dynamicbase -Wl,--nxcompat \ + -Wl,--file-alignment,0x200 -Wl,--section-alignment,0x1000 -Wl,--stack,0x100000 \ + -Wl,--entry,DriverEntry -nostartfiles -nostdlib -o $(1_TARGET) \ + $(1_OBJECTS) -lntoskrnl -lhal + +$(2_TARGET): .deps-built $(2_OBJECTS) + $(CXX) $(CFLAGS) -Wl,--subsystem,native -Wl,--image-base,0x140000000 -Wl,--dynamicbase -Wl,--nxcompat \ + -Wl,--file-alignment,0x200 -Wl,--section-alignment,0x1000 -Wl,--stack,0x100000 \ + -Wl,--entry,DriverEntry@8 -nostartfiles -nostdlib -o $(2_TARGET) \ + $(2_OBJECTS) -lntoskrnl -lhal diff --git a/README.md b/README.md new file mode 100644 index 0000000..980494c --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# Mingw64 [D]river [D]evelopment [K]it - Template + +A demonstration on how to compile Windows kernel drivers using Mingw64. + +## How? + +You will need an modern Mingw64-GCC toolchain. +Do not use any broken toolchains like the one shipped with debian-10. +Instead either use Zeranoe's build script with `make deps` or use your own. + +## What? + +1. ddk-template: plain and stupid ddk example +2. ddk-template-cplusplus: same, but in C++, including a very complex class + +## Build and Test + +Build all examples with a Mingw64 toolchain using Zeranoe's build script: + +`` +make all +`` + +Build all examples with your own Mingw64 toolchain: + +`` +make all CC=path/to/bin/x86_64-w64-mingw32-gcc DDK_INCLUDE_DIR=path/to/include/ddk +`` + +Build Mingw64 only: + +`` +make deps +`` diff --git a/ddk-template-cplusplus.cpp b/ddk-template-cplusplus.cpp new file mode 100644 index 0000000..3ab6257 --- /dev/null +++ b/ddk-template-cplusplus.cpp @@ -0,0 +1,50 @@ +#include <ntddk.h> + +#include <vector> + +class TestSmth +{ +public: + TestSmth() {} + void doSmth(void) + { + DbgPrint("%s\n", "Hello Class!"); + } +}; + +static void test_cplusplus(void) +{ + TestSmth t; + t.doSmth(); +} + +extern "C" +{ + +DRIVER_INITIALIZE DriverEntry; +DRIVER_UNLOAD DriverUnload; + +NTSTATUS DriverEntry( + _In_ struct _DRIVER_OBJECT *DriverObject, + _In_ PUNICODE_STRING RegistryPath +) +{ + DbgPrint("%s\n", "Hello ring0!"); + + /* support for service stopping */ + DriverObject->DriverUnload = DriverUnload; + + test_cplusplus(); + + return STATUS_SUCCESS; +} + +VOID +DriverUnload( + _In_ struct _DRIVER_OBJECT *DriverObject +) +{ + DbgPrint("%s\n", "Bye ring0!"); +} + +} diff --git a/ddk-template.c b/ddk-template.c new file mode 100644 index 0000000..9374b1f --- /dev/null +++ b/ddk-template.c @@ -0,0 +1,25 @@ +#include <ntddk.h> + +DRIVER_INITIALIZE DriverEntry; +DRIVER_UNLOAD DriverUnload; + +NTSTATUS DriverEntry( + _In_ struct _DRIVER_OBJECT *DriverObject, + _In_ PUNICODE_STRING RegistryPath +) +{ + DbgPrint("%s\n", "Hello ring0!"); + + /* support for service stopping */ + DriverObject->DriverUnload = DriverUnload; + + return STATUS_SUCCESS; +} + +VOID +DriverUnload( + _In_ struct _DRIVER_OBJECT *DriverObject +) +{ + DbgPrint("%s\n", "Bye ring0!"); +} diff --git a/mingw-w64-build b/mingw-w64-build new file mode 160000 +Subproject 15801def126642e4e2d4e2220e0d344ed8f1239 |