Provide option to version info in generated .sys file (#2401)

* Provide option to version info in generated .sys file

Signed-off-by: Alan Jowett <alanjo@microsoft.com>

* PR feedback

Signed-off-by: Alan Jowett <alanjo@microsoft.com>

---------

Signed-off-by: Alan Jowett <alanjo@microsoft.com>
This commit is contained in:
Alan Jowett 2023-05-01 18:39:42 -06:00 коммит произвёл GitHub
Родитель ccc99b2d02
Коммит 332fa2ba1e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 108 добавлений и 34 удалений

Просмотреть файл

@ -11,7 +11,7 @@ function(add_bpftonative_command file_name kernel_mode unsafe_program)
set (output_file ${output_dir}/${file_name}_um.dll)
endif()
set(command_line ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${output_dir}/Convert-BpfToNative.ps1 -IncludeDir ${CMAKE_SOURCE_DIR}/include -ProgramName ${file_name} -OutDir ${output_dir} -Platform x64 -Configuration $(Configuration) -KernelMode ${kernel_mode})
set(command_line ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${output_dir}/Convert-BpfToNative.ps1 -IncludeDir ${CMAKE_SOURCE_DIR}/include -FileName ${file_name} -OutDir ${output_dir} -Platform x64 -Configuration $(Configuration) -KernelMode ${kernel_mode})
set (input_file ${output_dir}/${file_name}.o)
if (${unsafe_program})

Просмотреть файл

@ -25,7 +25,7 @@ image that can be loaded and used as an eBPF program.
From the root of the eBPF-For-Windows project (from a VS Developer Command Prompt), after building the project, run:
```
powershell scripts\Convert-BpfToNative.ps1 -ProgramName my_program
powershell scripts\Convert-BpfToNative.ps1 -FileName my_program
```
Where my_program is the name of your BPF program without the extension. This will produce a native image in x64\Release
with name of my_program.sys.

Просмотреть файл

@ -17,8 +17,8 @@ if ($FileName -eq "bpf")
$ProgramType = "xdp"
}
.\Convert-BpfToNative.ps1 -ProgramName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $KernelConfiguration -KernelMode $True
.\Convert-BpfToNative.ps1 -ProgramName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $Configuration -KernelMode $False
.\Convert-BpfToNative.ps1 -FileName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $KernelConfiguration -KernelMode $True
.\Convert-BpfToNative.ps1 -FileName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $Configuration -KernelMode $False
Pop-Location

Просмотреть файл

@ -118,8 +118,8 @@
<Command>
clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c %(Filename).c -o $(OutputPath)%(Filename).o
pushd $(OutDir)
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false
popd
</Command>
<Outputs>$(OutputPath)%(Filename).o;$(OutputPath)%(Filename)_um.dll;$(OutputPath)%(Filename).sys</Outputs>
@ -144,8 +144,8 @@
<Command>
clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c unsafe\%(Filename).c -o $(OutputPath)%(Filename).o
pushd $(OutDir)
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true
popd
</Command>
<Outputs>$(OutputPath)%(Filename).o</Outputs>
@ -160,8 +160,8 @@
<Command>
clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c unsafe\%(Filename).c -o $(OutputPath)%(Filename).o
pushd $(OutDir)
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true
powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true
popd
</Command>
<Outputs>$(OutputPath)%(Filename).o</Outputs>

Просмотреть файл

@ -1,7 +1,74 @@
# Copyright (c) Microsoft Corporation
# SPDX-License-Identifier: MIT
param([parameter(Mandatory = $true)] [string] $ProgramName,
<#
.SYNOPSIS
Generate a native driver from a BPF program.
.DESCRIPTION
This script generates a native driver (.sys file) from a BPF program. The native program can be used to load BPF
programs into the kernel when Hypervisor-protected Code Integrity (HVCI) is enabled.
.PARAMETER FileName
Specifies the name of the ELF file containing the BPF program.
.PARAMETER Type
Specifies the type of the BPF program. Valid values include "xdp", "bind", "sockops", and other BPF program types.
.PARAMETER IncludeDir
Specifies the directory containing the bpf2c include files.
.PARAMETER BinDir
Specifies the directory containing the bpf2c binaries.
.PARAMETER OutDir
Specifies the directory where the generated driver will be placed.
.PARAMETER Platform
Specifies the target platform. Valid values include "x64".
.PARAMETER Configuration
Specifies the build configuration. Valid values include "Release", "FuzzerDebug", and "Debug".
.PARAMETER SkipVerification
Specifies whether to skip verification of the generated driver. This parameter is only supported for Debug builds.
.PARAMETER KernelMode
Specifies whether to generate a kernel-mode driver. If this parameter is false, a user DLL will be generated.
.PARAMETER ResourceFile
Specifies the path to a resource file to embed in the generated driver.
.EXAMPLE
.\Convert-BpfToNative.ps1 -FileName bindmonitor
This example generates a native driver from the BPF program bindmonitor.o.
.EXAMPLE
.\Convert-BpfToNative.ps1 -FileName bindmonitor -Type bind
This example generates a native driver from the BPF program bindmonitor.o. The
program type is set to "bind".
.EXAMPLE
.\Convert-BpfToNative.ps1 -FileName bindmonitor -Type bind -Configuration Debug
This example generates a native driver from the BPF program bindmonitor.o. The program type is set to "bind".
The driver is built in Debug configuration.
.EXAMPLE
.\Convert-BpfToNative.ps1 -FileName bindmonitor -Type bind -Configuration Debug -KernelMode $false
This example generates a user-mode DLL from the BPF program bindmonitor.o. The program type is set to "bind".
The driver is built in Debug configuration.
.NOTES
Author: Microsoft Corporation
Website: https://github.com/microsoft/ebpf-for-windows
#>
param([parameter(Mandatory = $true)] [string] $FileName,
[parameter(Mandatory = $false)] [string] $Type,
[parameter(Mandatory = $false)] [string] $IncludeDir = "$PSScriptRoot\..\include",
[parameter(Mandatory = $false)] [string] $BinDir = "$PSScriptRoot",
@ -9,7 +76,8 @@ param([parameter(Mandatory = $true)] [string] $ProgramName,
[parameter(Mandatory = $false)] [string] $Platform = "x64",
[ValidateSet("Release", "FuzzerDebug", "Debug")][parameter(Mandatory = $false)] [string] $Configuration = "Release",
[parameter(Mandatory = $false)] [bool] $SkipVerification = $false,
[parameter(Mandatory = $false)] [bool] $KernelMode = $true)
[parameter(Mandatory = $false)] [bool] $KernelMode = $true,
[parameter(Mandatory = $false)] [string] $ResourceFile = "")
Push-Location $OutDir
@ -17,8 +85,8 @@ $KernelModeProject = '___KERNEL_MODE_VCXPROJ___'
$UserModeProject = '___USER_MODE_VCXPROJ___'
# If program name ends with .o, remove the suffix
if ($ProgramName.EndsWith(".o")) {
$ProgramName = $ProgramName.Substring(0, $ProgramName.Length - 2)
if ($FileName.EndsWith(".o")) {
$FileName = $FileName.Substring(0, $FileName.Length - 2)
}
# SkipVerification is only supported for Debug builds.
@ -30,13 +98,13 @@ if ($null -eq (Get-Command 'msbuild.exe' -ErrorAction SilentlyContinue)) {
throw "Unable to locate msbuild.exe. This command needs to run within a 'Developer Command Prompt'"
}
$fileExists = Test-Path -Path ("$ProgramName.o")
$fileExists = Test-Path -Path ("$FileName.o")
if (!$fileExists) {
$errorString = "Can't find program file: " + "$ProgramName.o"
$errorString = "Can't find program file: " + "$FileName.o"
throw $errorString
}
$TempDir = "$OutDir\$ProgramName"
$TempDir = "$OutDir\$FileName"
if ($KernelMode) {
$TempDir += "_km"
@ -46,15 +114,15 @@ else {
}
mkdir -Force $TempDir
Copy-Item -Path "$ProgramName.o" -Destination "$TempDir\"
Copy-Item -Path "$FileName.o" -Destination "$TempDir\"
cd $TempDir
if ($KernelMode) {
$ProjectFile = "$ProgramName.vcxproj"
$ProjectFile = "$FileName.vcxproj"
Set-Content -Path $ProjectFile -Value $KernelModeProject
}
else {
$ProjectFile = "$ProgramName_um.vcxproj"
$ProjectFile = "$FileName_um.vcxproj"
Set-Content -Path $ProjectFile -Value $UserModeProject
}
@ -64,10 +132,10 @@ if ($PSBoundParameters.ContainsKey("Type")) {
$AdditionalOptions += " --type $Type"
}
msbuild /p:BinDir="$BinDir\" /p:OutDir="$OutDir\" /p:IncludeDir="$IncludeDir" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:ProgramName="$ProgramName" /p:AdditionalOptions="$AdditionalOptions" $ProjectFile
msbuild /p:BinDir="$BinDir\" /p:OutDir="$OutDir\" /p:IncludeDir="$IncludeDir" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:FileName="$FileName" /p:AdditionalOptions="$AdditionalOptions" /p:ResourceFile="$ResourceFile" $ProjectFile
if ($LASTEXITCODE -ne 0) {
throw "Build failed for $ProgramName.o"
throw "Build failed for $FileName.o"
}
Pop-Location

Просмотреть файл

@ -37,9 +37,9 @@
<MinimumVisualStudioVersion>12.0</MinimumVisualStudioVersion>
<Configuration>Debug</Configuration>
<Platform Condition="'$(Platform)' == ''">Win32</Platform>
<RootNamespace>$(ProgramName)</RootNamespace>
<RootNamespace>$(FileName)</RootNamespace>
<WindowsTargetPlatformVersion>$(LatestTargetPlatformVersion)</WindowsTargetPlatformVersion>
<ProjectName>$(ProgramName)_km</ProjectName>
<ProjectName>$(FileName)_km</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup>
@ -60,11 +60,11 @@
<PropertyGroup />
<PropertyGroup>
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
<TargetName>$(ProgramName)</TargetName>
<TargetName>$(FileName)</TargetName>
</PropertyGroup>
<ItemDefinitionGroup>
<PreBuildEvent>
<Command>$(BinDir)bpf2c --bpf $(ProgramName).o --sys $(AdditionalOptions) &gt;$(ProgramName)_driver.c</Command>
<Command>$(BinDir)bpf2c --bpf $(FileName).o --sys $(AdditionalOptions) &gt;$(FileName)_driver.c</Command>
</PreBuildEvent>
<ClCompile>
<AdditionalIncludeDirectories>$(IncludeDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
@ -83,9 +83,12 @@
<FilesToPackage Include="$(TargetPath)" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(ProgramName)_driver.c" />
<ClCompile Include="$(FileName)_driver.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<ItemGroup Condition="$(ResourceFile)!=''">
<ResourceCompile Include="$(ResourceFile)" />
</ItemGroup>
</Project>

Просмотреть файл

@ -19,15 +19,15 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(ProgramName)_dll.c" />
<ClCompile Include="$(FileName)_dll.c" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{1baf5f74-b71f-4088-8677-f27070302b59}</ProjectGuid>
<RootNamespace>$(ProgramName)dll</RootNamespace>
<RootNamespace>$(FileName)dll</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>$(ProgramName)_um</ProjectName>
<ProjectName>$(FileName)_um</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup>
@ -47,7 +47,7 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<LinkIncremental>false</LinkIncremental>
<TargetName>$(ProgramName)_um</TargetName>
<TargetName>$(FileName)_um</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
@ -64,7 +64,7 @@
<EnableUAC>false</EnableUAC>
</Link>
<PreBuildEvent>
<Command>$(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) &gt;$(ProgramName)_dll.c</Command>
<Command>$(BinDir)bpf2c.exe --bpf $(FileName).o --dll $(AdditionalOptions) &gt;$(FileName)_dll.c</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='FuzzerDebug|x64'">
@ -82,7 +82,7 @@
<EnableUAC>false</EnableUAC>
</Link>
<PreBuildEvent>
<Command>$(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) &gt;$(ProgramName)_dll.c</Command>
<Command>$(BinDir)bpf2c.exe --bpf $(FileName).o --dll $(AdditionalOptions) &gt;$(FileName)_dll.c</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -102,9 +102,12 @@
<EnableUAC>false</EnableUAC>
</Link>
<PreBuildEvent>
<Command>$(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) &gt;$(ProgramName)_dll.c</Command>
<Command>$(BinDir)bpf2c.exe --bpf $(FileName).o --dll $(AdditionalOptions) &gt;$(FileName)_dll.c</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup Condition="$(ResourceFile)!=''">
<ResourceCompile Include="$(ResourceFile)" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>