1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file load.lua
--
-- imports
import("core.project.config")
import("core.base.semver")
import("os.winver", {alias = "os_winver"})
function _add_linkflags(target, t, ...)
local args = table.pack(...)
if target:has_tool("ld", "clang", "clangxx") then
for i, flag in ipairs(args) do
if type(flag) == "string" then
args[i] = "-Wl," .. flag
end
end
end
target:add(t, table.unpack(args))
end
function _add_ldflags(target, ...)
_add_linkflags(target, "ldflags", ...)
end
function _add_shflags(target, ...)
_add_linkflags(target, "shflags", ...)
end
-- load for umdf driver
function driver_umdf(target)
-- set kind
target:set("kind", "shared")
-- add links
target:add("links", "ntdll", "OneCoreUAP", "mincore", "ucrt")
_add_shflags(target, "-NODEFAULTLIB:kernel32.lib", "-NODEFAULTLIB:user32.lib", "-NODEFAULTLIB:libucrt.lib", {force = true})
-- add subsystem
if not target:values("windows.subsystem") then
target:values_set("windows.subsystem", "windows," .. os_winver.subsystem(winver))
end
-- set default driver entry if does not exist
local entry = false
for _, ldflag in ipairs(target:get("shflags")) do
ldflag = ldflag:lower()
if ldflag:find("[/%-]entry:") then
entry = true
break
end
end
-- get wdk
local wdk = target:data("wdk")
if not entry and semver.compare(wdk.umdfver, "2.0") >= 0 then
target:add("links", "WdfDriverStubUm")
end
end
function _kernel_driver_base(target, default_entrypoint)
-- set kind
target:set("kind", "binary")
-- set filename: xxx.sys
target:set("filename", target:basename() .. ".sys")
-- add links
local winver = target:values("wdk.env.winver") or config.get("wdk_winver")
if winver and os_winver.version(winver) >= os_winver.version("win8") then
target:add("links", "BufferOverflowFastFailK")
else
target:add("links", "BufferOverflowK")
end
-- compile as kernel driver
if not target:has_tool("cc", "clang", "clangxx") then
target:add("cxflags", "-kernel", {force = true})
else
-- emulate kernel mode https://learn.microsoft.com/en-us/cpp/build/reference/kernel-create-kernel-mode-binary?view=msvc-170
target:add("cxxflags", "-fno-exceptions", "-fno-rtti")
target:add("defines", "_KERNEL_MODE=1")
end
_add_ldflags(target, "-kernel", "-driver", "-nodefaultlib", {force = true})
-- add subsystem
if not target:values("windows.subsystem") then
target:values_set("windows.subsystem", "native," .. os_winver.subsystem(winver))
end
-- set default driver entry if does not exist
local has_entry = false
for _, ldflag in ipairs(target:get("ldflags")) do
if type(ldflag) == "string" then
ldflag = ldflag:lower()
if ldflag:find("[/%-]entry:") then
has_entry = true
break
end
end
end
if not has_entry then
local arch = (target:is_arch("x86") and "@8" or "") target:add("links", "WdfDriverEntry")
_add_ldflags(target, "-entry:" .. default_entrypoint .. arch, {force = true})
end
end
-- load for kmdf driver
function driver_kmdf(target)
target:add("links", "ntoskrnl", "hal", "wmilib", "WdfLdr", "ntstrsafe", "wdmsec")
_kernel_driver_base(target, "FxDriverEntry")
end
-- load for wdm driver
function driver_wdm(target)
target:add("links", "ntoskrnl", "hal", "wmilib", "ntstrsafe")
_kernel_driver_base(target, "GsDriverEntry")
end
|